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 Methods Practice Makes Perfect!

Brandon Keene
Brandon Keene
7,217 Points

How do I subtract one variable from another and use the result as a third variable?

This is my first attempt at a basic ruby program, and I'm missing one final element. I feel like it is staring me in the face, but I can't see it. I want to ask for the user's birth year, then ask for the current year, and then tell them their age. <br> <br>Here's what I have so far:

<br>puts "Hi, please tell us a little bit about yourself."
<br>puts "What is your name?"
<br>name = gets.chomp
<br>puts "What year were you born?"
<br>born = gets.to_i
<br>puts "What year is it right now?"
<br>year = gets.to_i

<br>age = "year" - "born"

<br>puts "So, your name is #{name} and you were born in #{born} and you are #{age} years old."

<br> <br>Thanks!

3 Answers

You could still do the math in a string. You just have to use interpolation.

So you could do this;

age = "#{year - born}"

or

age = %(#{year - born})

You could also avoid having to ask them for the year my doing;

year = Time.now.year
Brandon Keene
Brandon Keene
7,217 Points

Jared, Thanks a lot, those are also excellent options!

Would you mind telling me how to post code chunks as you have them (and as mine seem to have become)? I couldn't find the appropriate info in the markdown cheatsheet.

Thanks again!

Brandon Keene

I edited your code so it would show correctly.

when you have text and then code, make sure there is a full blank line about and below the code block.

And right below the text box you use for commenting and post, there is a link in bold that says Markdown Cheatsheet

Brandon Keene
Brandon Keene
7,217 Points

Jared, As a mod, I figured that might've been your doing. I appreciate it, and I'll take another look at the cheatsheet.

puts "Thanks for everything!"

Let's see if that worked...

You're welcome and it worked!

Chel Kim
Chel Kim
1,736 Points

Thanks Jared! You just made Ruby more fun for me :D

Tom McKay
Tom McKay
4,714 Points

hey, i haven't done a ton of ruby but if I'm not mistaken you need to remove the quotes from around "year" and "born", as you are currently subtracting the string "born" from the string "year" rather than your variables year and born

so the 2nd to last line would be: age = year - born

Brandon Keene
Brandon Keene
7,217 Points

Tom, See what I mean about staring me right in the face?? Thanks so much, that solved it!