Home > Software design >  ¿Add [""] to ruby string?
¿Add [""] to ruby string?

Time:10-01

How can I convert this:

https://myimage.com

to this in ruby?

["https://myimage.com"]

I've tried with join with no luck...

Thanks

CodePudding user response:

You have to escape characaters that are reserved.

str = "https://myimage.com"
res = "[\""   str   "\"]"

CodePudding user response:

It's not very clear what you're looking to accomplish. You can't really turn https://myimage.com into anything, since it is not valid Ruby syntax.

However, if you first have it as a string, then you can easily put it inside of an array like this:

url    = 'https://myimage.com'
result = [url]
puts result.inspect
#=> ["https://myimage.com"]

Or if instead you want a string as your result, then here yah go:

url    = 'https://myimage.com'
result = '["'   url   '"]'
puts result
#=> ["https://myimage.com"]
  • Related