Reputation: 235
I am attempting to create a string using:
puts '["#{follower.screen_name}"]'
I want the output to be
["DailySanJose"]
where DailySanJose
is the value of follower.screen_name
. However, the current output is
["#{follower.screen_name}"]
Any help greatly appreciated.
Upvotes: 1
Views: 75
Reputation: 110755
To round out the options, you could also use a heredoc:
howdy = "hi"
puts <<_
["#{howdy}"]
_
# ["hi"]
Upvotes: 0
Reputation: 211740
If you're trying to make something like JSON output:
require 'json'
test = "DailySanJose"
JSON.dump([ test ])
# => "[\"DailySanJose\"]"
The advantage here is this accounts for strings like The "Test"
which requires double quoting. Don't be confused by the \"
part, as when you print that it comes out exactly as-is:
puts JSON.dump([ test ])
# => ["DailySanJose"]
There's also a few other simple ways:
[ test ].inspect
# => "[\"DailySanJose\"]"
Upvotes: 1
Reputation: 1276
Edit
you can do it this way with double quotes
puts %{["#{follower.screen_name}"]}
Upvotes: -1
Reputation: 997
You could use a %Q to not have to worry about escaping quotes
such as
%Q(["#{follower.screen_name}"])
This will escape the quotes for you automatically and do the string interpolation, that way you don't have to worry about it yourself.
Upvotes: 3
Reputation: 35788
Interpolation (#{}
) only works in double quoted strings.
Try this:
puts "[\"#{follower.screen_name}\"]"
I'm including the double quotes in the string by escaping them with the \
(backslash) character.
Upvotes: 4