Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

SImple Ruby Program

Im trying to run this simple program to practice Ruby but I keep getting an error, I dont understand why, any help would be great

code below..running it in workspace

print "What is your age?"
age = gets.chomp

if age >18 
    print " You are old enough to play"

 elsif age <18 
    print " you are too young"

else print" you did not give a number"

end

EDIT: I edited your code so it highlights. Look what i did to achieve this effect by press EDIT. -Aurelian

Hi, does it tell you on what line did you get an error?

Thanks for editing like that, I didn't know how to do that.

I get this error

methods.rb:4:in >': comparison of String with 18 failed (ArgumentError) from methods.rb:4:in<main>'

1 Answer

Try this:

age = gets.chomp.to_i

This will convert the string you're reading into an integer, which allows legal comparisons with other integers.

Oh i think i remember it now. Its basically if you want to get an number , you need to always put " .to_i " right?

Yeah, this goes through the string and takes and interprets all numbers until it encounters a non-number, at which point it returns the number it got until this point.

That worked! thanks!