Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Ruby (http://www.velocityreviews.com/forums/f66-ruby.html)
-   -   New user - very basic question (http://www.velocityreviews.com/forums/t847981-new-user-very-basic-question.html)

Ashley Wharton 02-07-2008 10:00 PM

New user - very basic question
 
[Note: parts of this message were removed to make it a legal post.]

Hi - sorry to bother with such a basic question, but is there a better way
to write the following: (before I move on I want to make sure I am following
best practice)

def require_number number_cur
puts number_cur
reply = gets.to_i

while reply < 100
reply = reply + 1
puts reply
end

if reply > 100
true
require_number 'Please enter a number less than 100: '
end
end
require_number 'Enter a number: '

Thanks so much!


Benjamin Stiglitz 02-07-2008 10:10 PM

Re: New user - very basic question
 
Not elegant, but better:

def require_number(prompt, max)
print prompt
reply = gets.to_i
if reply >= max then reply = require_number "Please enter a number
less than #{max}: ", max end
reply
end

require_number 'Please enter a number: ', 100
--
Posted via http://www.ruby-forum.com/.


Ashley Wharton 02-07-2008 10:42 PM

Re: New user - very basic question
 
[Note: parts of this message were removed to make it a legal post.]

On Feb 7, 2008 5:10 PM, Benjamin Stiglitz <ben@tanjero.com> wrote:

> Not elegant, but better:
>
> def require_number(prompt, max)
> print prompt
> reply = gets.to_i
> if reply >= max then reply = require_number "Please enter a number
> less than #{max}: ", max end
> reply
> end
>
> require_number 'Please enter a number: ', 100



Thank you for taking the time - could I trouble you further? If I want
the "reply" to iterate through until it reaches 100 could just add the
"while" to your code as below?

def require_number(prompt, max)
print prompt
reply = gets.to_i
if reply >= max then reply = require_number "Please enter a number
less than #{max}: ", max end
while reply < max
reply = reply + 1
puts reply
end
end
require_number 'Please enter a number: ', 100

>
> --
> Posted via http://www.ruby-forum.com/.
>
>



Benjamin Stiglitz 02-07-2008 11:13 PM

Re: New user - very basic question
 
> Thank you for taking the time - could I trouble you further? If I want
> the "reply" to iterate through until it reaches 100 could just add the
> "while" to your code as below?


> while reply < max
> reply = reply + 1
> puts reply
> end


Even better is
reply.upto(max - 1) { |x| puts x }

-Ben

--
Posted via http://www.ruby-forum.com/.


Ashley Wharton 02-08-2008 01:14 AM

Re: New user - very basic question
 
[Note: parts of this message were removed to make it a legal post.]

Thank you very much! Take care,

ashley

On Feb 7, 2008 6:13 PM, Benjamin Stiglitz <ben@tanjero.com> wrote:

> > Thank you for taking the time - could I trouble you further? If I want
> > the "reply" to iterate through until it reaches 100 could just add the
> > "while" to your code as below?

>
> > while reply < max
> > reply = reply + 1
> > puts reply
> > end

>
> Even better is
> reply.upto(max - 1) { |x| puts x }
>
> -Ben
>
> --
> Posted via http://www.ruby-forum.com/.
>
>




All times are GMT. The time now is 10:45 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.