New user - very basic question

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!

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

On Feb 7, 2008 5:10 PM, Benjamin S. [email protected] wrote:

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

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

Thank you very much! Take care,

ashley

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs