# New user - very basic question

**URL:** <https://www.ruby-forum.com/t/new-user-very-basic-question/128469>\
**Category:** Ruby\
**Created:** [February 7, 2008, 11:01pm UTC](https://www.ruby-forum.com/t/new-user-very-basic-question/128469 "2008-02-07T23:01:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ashleyw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9bcb4/32.png) [@ashleyw](https://www.ruby-forum.com/u/ashleyw)\
**Post date:** [February 7, 2008, 11:01pm UTC](https://www.ruby-forum.com/t/new-user-very-basic-question/128469/1 "2008-02-07T23:01:07Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![ashleyw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9bcb4/32.png) [@ashleyw](https://www.ruby-forum.com/u/ashleyw)\
**Post date:** [February 7, 2008, 11:10pm UTC](https://www.ruby-forum.com/t/new-user-very-basic-question/128469/2 "2008-02-07T23:10:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ashleyw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9bcb4/32.png) [@ashleyw](https://www.ruby-forum.com/u/ashleyw)\
**Post date:** [February 7, 2008, 11:44pm UTC](https://www.ruby-forum.com/t/new-user-very-basic-question/128469/3 "2008-02-07T23:44:14Z")

</div>

On Feb 7, 2008 5:10 PM, Benjamin S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![ashleyw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9bcb4/32.png) [@ashleyw](https://www.ruby-forum.com/u/ashleyw)\
**Post date:** [February 8, 2008, 12:13am UTC](https://www.ruby-forum.com/t/new-user-very-basic-question/128469/4 "2008-02-08T00:13:29Z")

</div>

> 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

---

<div class="post-metadata">

**Author:** ![ashleyw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9bcb4/32.png) [@ashleyw](https://www.ruby-forum.com/u/ashleyw)\
**Post date:** [February 8, 2008, 2:15am UTC](https://www.ruby-forum.com/t/new-user-very-basic-question/128469/5 "2008-02-08T02:15:42Z")

</div>

Thank you very much! Take care,

ashley
