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) How Ruby Works Input and Output

ruby returns Hello Jason when typing gets but how is name still equal to Jason if name = gets

When you type name = gets puts "Hello #{ name}" I cant understand how Ruby knows to display Hello Jason when you have changed the value of name from Jason to gets. very confused please explain.

3 Answers

Sage Elliott
Sage Elliott
30,003 Points

If you watch the video, you will see that Jason has to input his name "Jason" before the program returns Hello, Jason".

That's what the gets does. It prompts for user input. In this example: name = gets #At this point it doesn't equal Jason, but once the program runs and prompts for user input it will change. When Jason enters his name at the prompt it then stores that value(Jason) into the value name.

O MY GOSH!!! you dont know how confused i was. As a girlie girl I was getting ready to cry lol thx!

Hi Niger, I will explain it the way I understand it. Ruby takes the input from the user through "gets" (gets means get string) and assigns it to the variable "name". So since name = gets... and gets == "Jason" (which is the user input), that means that name gets assigned the value "Jason" and when you now say, puts "Hello #{ name}", it replaces the #{name} with its current value which is "Jason" and gives output "Hello Jason". I hope this makes sense.

Sage Elliott
Sage Elliott
30,003 Points

Anytime! That's what the forum is for! We all get stuck on stuff, especially when we're learning a new concept!