Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.ruby > #3489 > unrolled thread

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

Started byRichard Sandoval <skolopen@yahoo.com>
First post2011-04-25 22:16 -0500
Last post2011-04-26 12:43 -0500
Articles 6 — 4 participants

Back to article view | Back to comp.lang.ruby


Contents

  Can I Add quotes to values in a array? or include quotes.. Richard Sandoval <skolopen@yahoo.com> - 2011-04-25 22:16 -0500
    Re: Can I Add quotes to values in a array? or include quotes.. Nikita Baksalyar <n.baksalyar@yandex.ru> - 2011-04-25 23:49 -0500
    Re: Can I Add quotes to values in a array? or include quotes.. 7stud -- <bbxx789_05ss@yahoo.com> - 2011-04-26 12:26 -0500
      Re: Can I Add quotes to values in a array? or include quotes.. Alexander McMillan <alexandermcmillan@hotmail.com> - 2011-04-26 12:50 -0500
        Re: Can I Add quotes to values in a array? or include quotes.. 7stud -- <bbxx789_05ss@yahoo.com> - 2011-04-26 16:30 -0500
    Re: Can I Add quotes to values in a array? or include quotes.. 7stud -- <bbxx789_05ss@yahoo.com> - 2011-04-26 12:43 -0500

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

FromRichard Sandoval <skolopen@yahoo.com>
Date2011-04-25 22:16 -0500
SubjectCan I Add quotes to values in a array? or include quotes..
Message-ID<1564dc8f34b4628de596a92894889790@ruby-forum.com>
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/.

[toc] | [next] | [standalone]


#3498

FromNikita Baksalyar <n.baksalyar@yandex.ru>
Date2011-04-25 23:49 -0500
Message-ID<4c279366f960dad42023b4939a13c8c6@ruby-forum.com>
In reply to#3489
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/.

[toc] | [prev] | [next] | [standalone]


#3525

From7stud -- <bbxx789_05ss@yahoo.com>
Date2011-04-26 12:26 -0500
Message-ID<edab0fd90b02232c2dcc58120e57da2c@ruby-forum.com>
In reply to#3489
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/.

[toc] | [prev] | [next] | [standalone]


#3527

FromAlexander McMillan <alexandermcmillan@hotmail.com>
Date2011-04-26 12:50 -0500
Message-ID<BAY152-w12762B907B23D418DC84EAA6990@phx.gbl>
In reply to#3525

 

> Date: Wed, 27 Apr 2011 02:26:50 +0900
> From: bbxx789_05ss@yahoo.com
> Subject: Re: Can I Add quotes to values in a array? or include quotes..
> To: ruby-talk@ruby-lang.org
> 
> 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
> >
> 
> Use the escape slashes:
str = "\"Hello\""
put str 		 	   		  

[toc] | [prev] | [next] | [standalone]


#3533

From7stud -- <bbxx789_05ss@yahoo.com>
Date2011-04-26 16:30 -0500
Message-ID<4278d55832b686163d10e0a1b1de3e84@ruby-forum.com>
In reply to#3527
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/.

[toc] | [prev] | [next] | [standalone]


#3526

From7stud -- <bbxx789_05ss@yahoo.com>
Date2011-04-26 12:43 -0500
Message-ID<c2510a7a194d1b2d0e8d0bf724e30572@ruby-forum.com>
In reply to#3489
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/.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.ruby


csiph-web