1

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.

1
  • Question is not clear. puts does not create (return) a string. Do you want to create or output a string? Commented Apr 15, 2016 at 15:48

6 Answers 6

4

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.

Sign up to request clarification or add additional context in comments.

Comments

3

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.

Comments

1

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\"]"

10 Comments

This does work. It's worth mentioning, however, that the output of inspect is not considered reliable, and shouldn't be used for anything besides debugging. Using JSON is much better.
@Linuxios How is inspect not reliable? It's fundamental to Ruby and when using plain strings it works great. I agree that JSON is a better call if that's the intended goal, as the output is a lot more standardized.
I don't know about the reliability of the method, but pp would print a much nicer string representation of a larger data structure.
@StephenCarman pp depends on a subjective definition of "pretty", so that might not meet the requirements here. JSON's output is extremely consistent and standards based.
@tadman well, yes, beauty is in the eye of the beholder :-) I was just suggesting additional options and I think it's prettier. :-)
|
0

To round out the options, you could also use a heredoc:

howdy = "hi"
puts <<_
["#{howdy}"]
_
  # ["hi"]

Comments

0

You can use puts '["'+follower.screen_name+'"]'

Comments

-1

Edit

you can do it this way with double quotes

puts %{["#{follower.screen_name}"]}

2 Comments

This will work, but it doesn't generate the desired output, which must contain double quotes, according to the OP.
@Linuxios, you are saying, "This will work but it doesn't work." :-) Strike "will work, but it"?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.