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

filibonio hernandez
filibonio hernandez
2,826 Points

Need some help figuring out this code.

So what I'm basically trying to do is doing what Jason did but putting in the variable "Gets" to get an interaction. This is my code

name = gets puts "Hi my name is Filibonio Hernandez what is your name? #{name}"

years = gets puts "Cool #{name} would you like to know how old you would be in 5 years from now! put down how old you are. #{years}"

future = 5 puts "You are #{years} now, in #{future} you are going to be #{years + future}"

I need a little help understanding why is it not working? why is it that I have to hit enter a couple of times to get to the first line? and why the second line won't let me have a chance to put in the number of years?

thanks for your time.

2 Answers

Amir Eskandari
Amir Eskandari
9,153 Points

Hi filibonio,

To fix this error add the "to_i" method to the statement that gets a value for the year variable, so that we convert the input to an integer. This will allow us to add the value of the "future" variable to the value of the "years" variable. I have included all of the code below:

puts "Hi my name is Filibonio Hernandez what is your name?" 
name = gets.chomp

puts "Cool #{name} would you like to know how old you would be in 5 years from now! put down how old you are."
years = gets.chomp.to_i

future = 5 
puts "You are #{years} now, in #{future} you are going to be #{years+future}"

Let me know if you have any other questions!

filibonio hernandez
filibonio hernandez
2,826 Points

Hey thanks Amir that did it!

So if you don't mind me asking how did you get into web dev. you go to school? or practice on your on your own? or going to those coding boot camps like I'm going to end up looking into pretty soon.

Amir Eskandari
Amir Eskandari
9,153 Points

Awesome!

I got my degree in something unrelated, didn't enjoy my job, then I discovered programming a little while ago and I've been teaching myself. Lot's of good resources out there to teach yourself (like this site) as long as you commit to it and stick with it. I'm also fortunate to have a job that allows me to learn new stuff often.

In my opinion, as long as you enjoy learning every day you will eventually become a great programmer.

Amir Eskandari
Amir Eskandari
9,153 Points

Hi filibonio,

You are on the right track, but you just need to break apart your statements a little bit.

Break apart this line:

name = gets puts "Hi my name is Filibonio Hernandez what is your name? #{name}"

Into two different lines - one that "puts" the question and then one that "gets" the user's response and stores it in a variable:

puts "Hi my name is Filibonio Hernandez what is your name?"
name = gets.chomp 

Now that we have stored the user's input in the name variable, we can display it in the next "puts" statement. So, using the same technique as we did above, let's break the next line apart and change it from this:

years = gets puts "Cool #{name} would you like to know how old you would be in 5 years from now! put down how old you are. #{years}"

To this:

puts "Cool #{name} would you like to know how old you would be in 5 years from now! put down how old you are."
years = gets.chomp

Your last line looks great, you should just break it apart into to lines. So this line:

future = 5 puts "You are #{years} now, in #{future} you are going to be #{years + future}"

Should become something like this:

future = 5 
puts "You are #{years} now, in #{future} you are going to be #{years + future}"

I hope this helps answer your question.

filibonio hernandez
filibonio hernandez
2,826 Points

Hey Amir Eskandari Thanks for answering my question all seems to be working out but the last line is not registering gives me an error like this :

hello.rb:8:in +': no implicit conversion of Fixnum into String from hello.rb:8:in<main>'

my code:

puts "Hi my name is Filibonio Hernandez what is your name?" name = gets.chomp

puts "Cool #{name} would you like to know how old you would be in 5 years from now! put down how old you are." #{years} years = gets.chomp

future = 5 puts "You are #{years} now, in #{future} you are going to be #{years+future}"

Hey thanks again for helping me solve this