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
kj2
5,933 PointsSImple 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
kj2
5,933 PointsThanks 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
Maciej Czuchnowski
36,441 PointsTry this:
age = gets.chomp.to_i
This will convert the string you're reading into an integer, which allows legal comparisons with other integers.
Aurelian Spodarec
7,369 PointsOh i think i remember it now. Its basically if you want to get an number , you need to always put " .to_i " right?
Maciej Czuchnowski
36,441 PointsYeah, 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.
kj2
5,933 PointsThat worked! thanks!
Aurelian Spodarec
7,369 PointsAurelian Spodarec
7,369 PointsHi, does it tell you on what line did you get an error?