# New to ruby - how can I take this further?

**URL:** <https://www.ruby-forum.com/t/new-to-ruby-how-can-i-take-this-further/173729>\
**Category:** Ruby\
**Created:** [August 13, 2009, 10:56pm UTC](https://www.ruby-forum.com/t/new-to-ruby-how-can-i-take-this-further/173729 "2009-08-13T22:56:48Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![stooz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e47774/32.png) [@stooz](https://www.ruby-forum.com/u/stooz)\
**Post date:** [August 13, 2009, 10:56pm UTC](https://www.ruby-forum.com/t/new-to-ruby-how-can-i-take-this-further/173729/1 "2009-08-13T22:56:48Z")

</div>

Hi all

I’m new to programming, and new to Ruby.

I’ve written a little program listed below, a very simple math test, to  
get into the swing of things.

My question; could someone point out what I could do to make this more  
elegant, and any mistakes etc. - basically a critique would be most  
welcome.

I think this would give me some real help in understanding what I need  
to learn next.

Many thanks for your time

s.

puts “Please enter your name:”  
firstname = gets.chomp

number1 = rand(100)  
number2 = rand(100)

operatorarray = ["+", “-”]  
randop = rand(2)  
getoperator = operatorarray[randop]

question = number1.to\_s + getoperator + number2.to\_s

answer = eval “#{number1} #{getoperator} #{number2}”

puts “Welcome to MathTest!”

puts “#{firstname}” + “, here is your first question:”

puts question  
usranswer = gets.chomp

if  
eval “#{usranswer} == #{answer}”  
puts “Correct!”  
else  
puts "Incorrect. The answer is " + “#{answer}”  
end

---

<div class="post-metadata">

**Author:** ![stooz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e47774/32.png) [@stooz](https://www.ruby-forum.com/u/stooz)\
**Post date:** [August 13, 2009, 11:30pm UTC](https://www.ruby-forum.com/t/new-to-ruby-how-can-i-take-this-further/173729/2 "2009-08-13T23:30:41Z")

</div>

On Thu, Aug 13, 2009 at 10:56 PM, Stuart Cullum[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Hi all
> 
> I’m new to programming, and new to Ruby.
> 
> I’ve written a little program listed below, a very simple math test, to  
> get into the swing of things.
> 
> My question; could someone point out what I could do to make this more  
> elegant, and any mistakes etc. - basically a critique would be most  
> welcome.

This is how I’d do it straight away from your code:

puts “Please enter your name:”  
first\_name = gets.chomp

number1 = rand(100)  
number2 = rand(100)

# I’d use symbols for the operators

# also note the snake\_case convention

operator\_array = [:+, :-]

#removed some unnecessary variables  
operator = operator\_array[rand(2)]

# Cleaner (IMHO) with string interpolation,

# no need for .to\_s

question = “#{number1} #{operator} #{number2}”

# We know the operators are methods of the numbers

# so I’d rather send the method than eval a string

answer = number1.send(operator, number2)

puts “Welcome to MathTest!”

# String interpolation rather than concatenation

puts “#{first\_name}, here is your first question:”

puts question

# get the answer as an integer

user\_answer = gets.chomp.to\_i

#no need to eval here, just compare the ints  
if user\_answer == answer  
puts “Correct!”  
else  
puts "Incorrect. The answer is " + “#{answer}”  
end

The next step could be to put the questions in a loop, so you don’t  
need to type your name every time, and to count correct/incorrect  
answers and show a summary. Further, you could extend this to save to  
a file a count of correct/incorrect answers by name, and load this  
data at start and update it with the new session of answers.

Good luck !

Jesus.

---

<div class="post-metadata">

**Author:** ![stooz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e47774/32.png) [@stooz](https://www.ruby-forum.com/u/stooz)\
**Post date:** [August 13, 2009, 11:37pm UTC](https://www.ruby-forum.com/t/new-to-ruby-how-can-i-take-this-further/173729/3 "2009-08-13T23:37:18Z")

</div>

> Good luck !
> 
> Jesus.

Jesus

Thanks very much for your time. I will go and attempt a new version!

Best regards

s.

---

<div class="post-metadata">

**Author:** ![stooz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e47774/32.png) [@stooz](https://www.ruby-forum.com/u/stooz)\
**Post date:** [August 14, 2009, 11:45pm UTC](https://www.ruby-forum.com/t/new-to-ruby-how-can-i-take-this-further/173729/4 "2009-08-14T23:45:12Z")

</div>

On Fri, Aug 14, 2009 at 6:25 PM, Fabian  
Streitel[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > operator\_array = [:+, :-]
> 
> I personally prefer %w:
> 
> operator\_array = %w{+ -}
> 
> Especially when you get more entries, %w helps you save time and maintain  
> readability.

I use %w a lot, when I want Strings. In this case though, my  
preference is to use symbols,  
because they represent better what they are used for in this program.

> > puts "Incorrect. The answer is " + “#{answer}”

> you missed one! 🙂

Yep, good catch 🙂

Cheers,

Jesus.

---

<div class="post-metadata">

**Author:** ![stooz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e47774/32.png) [@stooz](https://www.ruby-forum.com/u/stooz)\
**Post date:** [August 14, 2009, 6:26pm UTC](https://www.ruby-forum.com/t/new-to-ruby-how-can-i-take-this-further/173729/5 "2009-08-14T18:26:47Z")

</div>

just some more suggestions:

puts “Please enter your name:”

> first\_name = gets.chomp
> 
> number1 = rand(100)  
> number2 = rand(100)
> 
> # I’d use symbols for the operators
> 
> # also note the snake\_case convention
> 
> operator\_array = [:+, :-]

I personally prefer %w:

operator\_array = %w{+ -}

Especially when you get more entries, %w helps you save time and  
maintain  
readability.  
Basically, what it does is, it splits the string in between the {} at  
every  
whitespace and  
returns an Array containing the Strings inbetween the whitespace.

#removed some unnecessary variables

> operator = operator\_array[rand(2)]
> 
> # Cleaner (IMHO) with string interpolation,
> 
> # no need for .to\_s
> 
> question = “#{number1} #{operator} #{number2}”
> 
> # We know the operators are methods of the numbers
> 
> # so I’d rather send the method than eval a string
> 
> answer = number1.send(operator, number2)

Also, you should steer clear of eval, since it is quite a security risk.  
Users could give you any Ruby code, even stuff that deletes files etc.  
I’ve found that most of the time you can get rid of it.

puts “Welcome to MathTest!”

> else  
> puts "Incorrect. The answer is " + “#{answer}”

you missed one! 🙂

puts “Incorrect. The answer is #{answer}”

> end

Keep it up! Hope you have lots of fun with Ruby!  
Greetz!
