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 Ruby Basics (Retired) Ruby Numbers Practicing with Numbers

Jonathan Leon
Jonathan Leon
18,813 Points

Hello can someone look into my code please?

I've written something different than in the video and it won't work, why is that? I wanted to print a question about the current year, register it as input and output it as age:

print "What year were you born in?"

birth = gets
year = 2015

puts "Your age is #{year - birth}"

5 Answers

Jonathan Leon
Jonathan Leon
18,813 Points

Now I'm in JavaScript basics and I've learnt that parseInt takes the numeric integer out of a string and returns it. So I'm pretty sure there's a similar operator in Ruby. Just need to find it out. Thanks for the help and have a great week :-)

Tommy Gebru
Tommy Gebru
30,164 Points

Hey Jonathan turns out the very next section on Ruby goes over how to turn a string into a numerical value with the method .to_i I tried the challenge in Workspaces and it worked ;)

Jonathan Leon
Jonathan Leon
18,813 Points

That's great to hear! I'm almost done with javascript basics and then on with Ruby, I hope the syntax wont mix up in my head.

John Fisher
John Fisher
7,974 Points

remember, 'gets' returns a string

Jonathan Leon
Jonathan Leon
18,813 Points

Alright so I need to define the substraction and "return" the value right?

Tommy Gebru
Tommy Gebru
30,164 Points

Hey Jonathan

I am guessing form the code below you would like to subtract birth from year. However both do not have numerical values.

print "What year were you born in? :"

birth = gets
year = 2015

puts "Your age is #{year - birth}"
Jonathan Leon
Jonathan Leon
18,813 Points

That's correct, so what would I need to add there in order for it to be "saved" as a number? I recall using gets for a string but here I'm asking for a fixnum, is there a different input for that? (maybe you have answered it but I cant see all of your reply sorry :("

John Fisher
John Fisher
7,974 Points

You're on the right track, like Tommy said, you are trying to subtract a string from an integer. There is a method you can use on 'gets' which will store it as a integer.

Tommy Gebru
Tommy Gebru
30,164 Points

Does this mean that gets, will return the value of a number as a string?

Jonathan Leon
Jonathan Leon
18,813 Points

I still didn't manage to solve it but I tried while reading the ruby.docs, it's something not covered yet but probably will be later so I'll come back to check on it and reply the answer for other curious newbies like me to see. I would love another hint though lol :)

Tommy Gebru
Tommy Gebru
30,164 Points

This is something I have not learned yet, but it seems as though gets will turn all input into strings! I did not know this, I spent a lot of time trying to make it work, but this is definitely a challenge I will come back to as well ;)

Edward Galindez
Edward Galindez
875 Points

I don't know if you've solved this yet. The answer is:

print "What year were you born in?"

birth = gets.to_i
year = 2015

puts "Your age is #{year - birth}"

The .to_i method turns a string into an integer. And if you wanted to turn an integer into a string you could use .to_s

I hope that helped!