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
···
--
Posted via http://www.ruby-forum.com/.
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
···
On Feb 7, 2008 5:10 PM, Benjamin Stiglitz <ben@tanjero.com> wrote:
--
Posted via http://www.ruby-forum.com/\.
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/\.
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/\.