Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Can I Add quotes to values in a array? or include quotes..

Reply
Thread Tools

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

 
 
Richard Sandoval
Guest
Posts: n/a
 
      04-26-2011
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?

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Nikita Baksalyar
Guest
Posts: n/a
 
      04-26-2011
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!

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
7stud --
Guest
Posts: n/a
 
      04-26-2011
Richard Sandoval 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

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"

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
7stud --
Guest
Posts: n/a
 
      04-26-2011
Note that you can use any delimiter with %q and %Q: {}, [], !!, so you
can use a delimiter that makes your code clear.

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Alexander McMillan
Guest
Posts: n/a
 
      04-26-2011


=20

> Date: Wed=2C 27 Apr 2011 02:26:50 +0900
> From: http://www.velocityreviews.com/forums/(E-Mail Removed)
> Subject: Re: Can I Add quotes to values in a array? or include quotes..
> To: (E-Mail Removed)
>=20
> Richard Sandoval wrote in post #995007:
> > When I puts that yaml file for that application field=2C I get App1=2CA=

pp2.
> > How can I add quotes to each application. I'd like my output to be
> > "App1=2CApp2" which includes the quotes in the output
> >

>=20
> Use the escape slashes:

str =3D "\"Hello\""
put str =

 
Reply With Quote
 
7stud --
Guest
Posts: n/a
 
      04-26-2011
Alex Mcmillan wrote in post #995162:
>>
>> Use the escape slashes:

> str = "\"Hello\""
> put str


Don't ever do that! Horrible.

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
/* #include <someyhing.h> */ => include it or do not include it?That is the question .... Andreas Bogenberger C Programming 3 02-22-2008 10:53 AM
PHP double quotes inside double quotes MSB Computer Support 11 10-21-2006 01:09 PM
Asp.NET Javascript string, want to pass '(single quotes' within '(single quotes) Chris ASP .Net 1 03-24-2006 09:03 PM
Quotes/Double Quotes in Image Control Chris White ASP .Net 1 09-22-2004 06:22 AM
Multiline quotes - escaping quotes - et al Lawrence Tierney Java 3 12-24-2003 05:12 PM



Advertisments