Can I Add quotes to values in a array? or include quotes

Please advise on my situation.

I have something like this

A Yaml file that contains

Application:

  • App1
  • App2

When I puts that yaml file for that application field, I get App1,App2.
How can I add quotes to each application. I’d like my output to be
“App1,App2” which includes the quotes in the output

I was thinking maybe through the use of gsub but I’m still new to ruby.
Could someone provide some aid?

Hello,

It’s simple:

require "yaml"

yaml = YAML::load("Application:\n  - App1\n  - App2")

puts '"' + yaml['Application'].join(', ') + '"'

That’s it. I hope it’ll help! :slight_smile:

Richard S. wrote in post #995007:

When I puts that yaml file for that application field, I get App1,App2.
How can I add quotes to each application. I’d like my output to be
“App1,App2” which includes the quotes in the output

In Ruby, two of the String constructors are ’ ’ and " ". So if you
create a string like this:

str = ‘hello’

and write:

puts str

then the output will be:

hello

Now, what if you want str to start with a dash and end with a dash? How
would you do that? Like this:

str = ‘-hello-’
puts str

and the output will be:

-hello-

Similarly, if you want the string to start with a double quote and end
with a double quote, then include a double quote before the ‘h’ and
after the ‘o’:

str = ‘“hello”’
puts str

and the output will be:

“hello”

But what if you try:

str = ““hello””
puts str

That is problematic because ruby first sees this:

str = “”

which sets str to a blank string. Then the rest of the line confuses
ruby, so ruby stops everything and gives you an error.

Sometimes mixing single quotes and double quotes gets confusing, so ruby
also provides two other string constructors: %q and %Q. The lower case
‘q’ is the same as single quotes, and the uppercase ‘Q’ is the same as
double quotes:

str = %q{“hello \n world”}
puts str

–output:–
“hello \n world”

str = %Q{“hello \n world”}
puts str

–output:–
“hello
world”

Note that the last example is almost equivalent to:

str = ““hello \n world””

except that ruby doesn’t first see this:

str = “”

and therefore ruby doesn’t get confused. Rather, the %Q allows ruby to
interpret the string as you intended.

If you are reading str from a file, then as Nikita Baksalyar showed you,
you have to create a new string that has a quote mark at the beginning
and end of the string. Here is another way to create the new string:

str = ‘hello’ #from file

new_str = %Q["#{str}"]
puts new_str

–output:–
“hello”

Use the escape slashes:
str = ““Hello””
put str

Note that you can use any delimiter with %q and %Q: {}, [], !!, so you
can use a delimiter that looks best.

Alex Mcmillan wrote in post #995162:

Use the escape slashes:
str = ““Hello””
put str

Horrible. Go back to the beginning and start over.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs