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 The Solution

My code for this challenge

print "when you include \\t in a double-quotes string in Ruby, the output looks like this:\nbefore\tafter\nWhen you include \\n in a double-quoted string, the output looks like this:\nbefore\nafter\n\nThe first \"before\"/\"after\" pair should have a single tab character between them,\nNOT space characters.\n"

correct ?

Jason Egipto
Jason Egipto
9,185 Points

Im guessing you used print instead of puts in order to practice the escape sequence a bit better right?

3 Answers

Hi, Ahmed!

Copied your code into Workspaces, and it runs just fine!

Here are some additional options:

puts 'When you include \t in a double-quoted string in Ruby, the output looks like this:'
puts "before\tafter"
puts 'When you include \n in a double-quoted string, the output looks like this:'
puts "before\nafter"

Alternately, we can make a Ruby heredoc like this:

puts <<-EOF
When you include \\t in a double-quoted string in Ruby, the output looks like this:
before\tafter
When you include \\n in a double-quoted string, the output looks like this:
before
after
EOF

It might not seem particularly profound to another reader, but my mind was just blown. I realized how many different ways there must be to arrive at the same console return, even with a simple program like this.

I did this

puts "When you include \\t in a double-quoted string in Ruby, the output looks like this:\nbefore\tafter"

puts "When you include \\n in a double-quoted string, the output looks like this:\nbefore\nafter"

I haven't learned about the Ruby heredoc yet.

# this is a quick tutorial on how to use escape sequences inside ruby. 

# When you include \t in a double-quoted string in Ruby, the output looks like this:

puts "\tYeer!"

# before  
puts "  Yeer!"
# =>    Yeer!

#after
puts "\tYeer!"
# =>    Yeer!


# When you include \n in a double-quoted string, the output looks like this:

puts "Yeer!, im feeling like,\n\"Woah\"!"

# before
puts "Yeer!, im feeling like,"
puts  ""Woah"!"

# =>  SyntaxError ((irb):1: syntax error, unexpected tCONSTANT, expecting end-of-input)
# => "Yeer!, im feeling like, "Woah"!"
...                           ^~~~

# after
puts Yeer!, im feeling like,\n\"Woah\"!"
# => Yeer!, im feeling like,
# => "Woah"!

# The first "before"/"after" pair should have a single tab character between them,
# NOT space characters.

puts "/tbefore and  after"
# =>   before and after


#before 
puts "  before and after"
# => "  before and  after"

#after
puts "\tbefore and  after"
# => "  before and after"