New user - correct way to think and write code

A

Ashley Wharton

[Note: parts of this message were removed to make it a legal post.]

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
-----------------------------------------------------------------------
 
R

Robert Klemme

2008/2/11 said:
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.
...

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

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
 
M

Morton Goldberg

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:

<code>
#! /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)
</code>

So do I.

Regards, Morton
 
A

Ashley Wharton

[Note: parts of this message were removed to make it a legal post.]

Thanks so much.
 
R

Robert Klemme

2008/2/12 said:
I interpret Robert as saying that he prefers something like:

<code>
#! /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)
</code>

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top