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

S K
S K
146 Points

newline after variable value

print "Please enter your name : " name = gets puts "Hello #{name} !"

The output is - Hello Jason !

How can we print the exclamation mark in the same line as the name. That is, how can we get "Hello Jason !" in the same line instead of two lines.

3 Answers

print "Please enter you name: " name = gets.chomp puts "Hello #{name}!"

Just use the .chomp as above to avoid the extra line and keep everything on the same line.

Dylan Shine
Dylan Shine
17,565 Points

print "Please enter your name : " name = gets puts "Hello #{name}!"...just omit the space

Taylor Porter
Taylor Porter
2,338 Points

Hi S K!

When you put spaces anywhere in your string, it will be in your output. Take this for example: print "Please enter your name : " name = gets puts "Hello #{name}!" It will output this - Hello Jason! But if you put "Please enter your name : " name = gets puts "Hello #{name} !", your output will be - Hello Jason !

This can be useful for a number of things. Like Jason explained in the video, if you add the spaces after the colon in "Please enter your name: []" (Square brackets stand for your cursor) it will look more readable, but if you put it like "Please enter your name:[]" it's harder to read.

I hope this helped!

-Taylor