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 trialEmmanuel Idiong
5,953 PointsInterpolation
when using the %Q syntax at the beginning of a new line code, can it replace the first line name = gets or its just some more fancy option of writing the line 2 code without having to type "Hello #{name}!"
1 Answer
Tommy Bregar
10,847 PointsThe %Q format can be traced back to the days of Perl scripts. Here's a good discussion of it on StackOverflow.
While it doesn't replace the "name = gets", you can use them together like this:
name = gets
puts %Q(Hello #{name})
The benefit of this type of string formatting is that you don't have to escape characters like / \ " ' to include them in the string:
chars = %Q(\/\"'"')
will give you the literal string \/\"'"' which is useful if you have a string that contains a lot of these potentially headache inducing characters.