# New user - correct way to think and write code

**URL:** <https://www.ruby-forum.com/t/new-user-correct-way-to-think-and-write-code/128730>\
**Category:** Ruby\
**Created:** [February 11, 2008, 2:59am UTC](https://www.ruby-forum.com/t/new-user-correct-way-to-think-and-write-code/128730 "2008-02-11T02:59:33Z")\
**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 11, 2008, 2:59am UTC](https://www.ruby-forum.com/t/new-user-correct-way-to-think-and-write-code/128730/1 "2008-02-11T02:59:33Z")

</div>

Hi - sorry to belabour the point, but I would appreciate any help or  
guidance that anyone could give relative to the preferred way of writing  
Ruby code. Thanks to some help earlier in the week from ruby-talk and  
from  
Kastner I now have two additional ways of achieving the same result. I  
am  
attempting to grasp the concept objects as well as learning ruby. Which  
syntax (or style) should I focus on, or does it not matter. From my  
limited  
understanding Kastner’s example would seem to be a cleaner use of  
objects,  
(or am I missing the point). Thanks for any help.

ashley

My starting point:

* * *

def require\_number number\_cur  
puts number\_cur  
reply = gets.to\_i

while reply \< 100000  
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: ’

Modified with input from Ruby-Talk

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.upto(max) { |x| puts x }  
end  
require\_number 'Please enter a number: ', 100000

* * *

With further help from Kastner:

MAX = 100000

def get\_number(prompt = “”)  
print prompt

# ruby methods return the last value

gets.to\_i  
end

def print\_from\_to(start, stop)  
start.upto(stop) do |number|  
puts number  
end  
end

loop do  
number = get\_number "Please enter a number less than #{MAX}: "  
next if number \>= MAX  
print\_from\_to(number, MAX)  
break  
end

---

<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 11, 2008, 1:26pm UTC](https://www.ruby-forum.com/t/new-user-correct-way-to-think-and-write-code/128730/2 "2008-02-11T13:26:07Z")

</div>

2008/2/11, Ashley W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Hi - sorry to belabour the point, but I would appreciate any help or  
> guidance that anyone could give relative to the preferred way of writing  
> Ruby code. Thanks to some help earlier in the week from ruby-talk and from  
> Kastner I now have two additional ways of achieving the same result. I am  
> attempting to grasp the concept objects as well as learning ruby. Which  
> syntax (or style) should I focus on, or does it not matter. From my limited  
> understanding Kastner’s example would seem to be a cleaner use of objects,  
> (or am I missing the point). Thanks for any help.

…

> def print\_from\_to(start, stop)  
> end  
> As far as I can see you logic is: read user input until it is valid  
> and then print from input to MAX. I would remove the printing from  
> the loop because that is much cleaner.

Kind regards

robert

---

<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 12, 2008, 1:34am UTC](https://www.ruby-forum.com/t/new-user-correct-way-to-think-and-write-code/128730/3 "2008-02-12T01:34:42Z")

</div>

On Feb 11, 2008, at 7:24 AM, Robert K. wrote:

> > gets.to\_i  
> > next if number \>= MAX  
> > print\_from\_to(number, MAX)  
> > break  
> > end
> 
> As far as I can see you logic is: read user input until it is valid  
> and then print from input to MAX. I would remove the printing from  
> the loop because that is much cleaner.

I interpret Robert as saying that he prefers something like:

`
#! /usr/bin/env ruby -w
MAX = 100_000
def get_number(prompt = “”)
print prompt
gets.to_i
end
def print_from_to(start, stop)
start.upto(stop) { |number| puts number }
end
number = MAX
while number >= MAX
number = get_number "Please enter a number less than #{MAX}: "
end
print_from_to(number, MAX)
`

So do I.

Regards, Morton

---

<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 12, 2008, 2:30am UTC](https://www.ruby-forum.com/t/new-user-correct-way-to-think-and-write-code/128730/4 "2008-02-12T02:30:23Z")

</div>

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 12, 2008, 8:36am UTC](https://www.ruby-forum.com/t/new-user-correct-way-to-think-and-write-code/128730/5 "2008-02-12T08:36:31Z")

</div>

2008/2/12, Morton G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > > print prompt  
> > > loop do  
> > > I interpret Robert as saying that he prefers something like:
> 
> So do I.

Actually I would have formulated it a bit different, i.e. applied the  
loop check after reading:

#! /bin/env ruby

MAX = 100\_000

def get\_number(prompt = “”)  
print prompt  
gets.to\_i  
end

def print\_from\_to(start, stop)  
start.upto(stop) { |number| puts number }  
end

begin  
number = get\_number "Please enter a number less than #{MAX}: "  
end until (0…MAX).include? number

# alternative in one line:

# number = get\_number "Please enter a number less than #{MAX}: " until

(0…MAX).include? number

print\_from\_to(number, MAX)

Kind regards

robert
