Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > strings that contain quote marks

Reply
Thread Tools

strings that contain quote marks

 
 
jonsteenbergen@gmail.com
Guest
Posts: n/a
 
      01-24-2008
Sorry if this post is a duplicate, I'm having problems with my posts
posting.

Hello,
I've searched through the Ruby documentation to try and figure this
out - no luck at all, also searched google to see if anyone had
written about this. Any help or direction on where to find the answer
would be much appreciated!

This is in a rails project, but I think the answer lies in Ruby's
string methods.
I've got an object that has a list of materials attached to it, so in
my model I grab all the materials and convert them into a string -
like this:

self.materials.collect{ |x| x.material.strip.humanize.split.map{ |w|
w.capitalize}.join(' ')}.join(', ')

So I break apart each material by word, capitalize it, put that
material string back together and then join all the materials by
commas.

This works great, except that some of the materials are books with the
titles surrounded by quotes -
Book "the Snowy Day," By Ezra Jack Keats

So my code above works, except for the first letter after the initial
"
Is there a method to deal with this, so that it correctly capitalizes
the first letter after the " if there is a quote? I've tried a lot of
different things, but nothing is giving me the results I need.

Anyone have any ideas?
Thanks!
 
Reply With Quote
 
 
 
 
Alex LeDonne
Guest
Posts: n/a
 
      01-24-2008
On Jan 24, 2008 10:59 AM, http://www.velocityreviews.com/forums/(E-Mail Removed)
<(E-Mail Removed)> wrote:
> Sorry if this post is a duplicate, I'm having problems with my posts
> posting.
>
> Hello,
> I've searched through the Ruby documentation to try and figure this
> out - no luck at all, also searched google to see if anyone had
> written about this. Any help or direction on where to find the answer
> would be much appreciated!
>
> This is in a rails project, but I think the answer lies in Ruby's
> string methods.
> I've got an object that has a list of materials attached to it, so in
> my model I grab all the materials and convert them into a string -
> like this:
>
> self.materials.collect{ |x| x.material.strip.humanize.split.map{ |w|
> w.capitalize}.join(' ')}.join(', ')
>
> So I break apart each material by word, capitalize it, put that
> material string back together and then join all the materials by
> commas.
>
> This works great, except that some of the materials are books with the
> titles surrounded by quotes -
> Book "the Snowy Day," By Ezra Jack Keats
>
> So my code above works, except for the first letter after the initial
> "
> Is there a method to deal with this, so that it correctly capitalizes
> the first letter after the " if there is a quote? I've tried a lot of
> different things, but nothing is giving me the results I need.
>
> Anyone have any ideas?
> Thanks!
>


Try changing split to split(/\b/), then change join(' ') to just join:

irb(main):001:0> material = 'Book "the Snowy Day," By Ezra Jack Keats'
=> "Book \"the Snowy Day,\" By Ezra Jack Keats"

irb(main):002:0> material.strip.split.map{ |w| w.capitalize }.join(' ')
=> "Book \"the Snowy Day,\" By Ezra Jack Keats"

irb(main):003:0> material.strip.split(/\b/).map{ |w| w.capitalize }.join
=> "Book \"The Snowy Day,\" By Ezra Jack Keats"


The split on /\b/ splits on word boundaries, preserving all the spaces
and punctuation between. That's why the join no longer needs spaces:

irb(main):004:0> material.strip.split(/\b/)
=> ["Book", " \"", "the", " ", "Snowy", " ", "Day", ",\" ", "By", " ",
"Ezra", " ", "Jack", " ", "Keats"]


-A

 
Reply With Quote
 
 
 
 
Axel Etzold
Guest
Posts: n/a
 
      01-24-2008

-------- Original-Nachricht --------
> Datum: Fri, 25 Jan 2008 00:59:58 +0900
> Von: "(E-Mail Removed)" <(E-Mail Removed)>
> An: (E-Mail Removed)
> Betreff: strings that contain quote marks


> Sorry if this post is a duplicate, I'm having problems with my posts
> posting.
>
> Hello,
> I've searched through the Ruby documentation to try and figure this
> out - no luck at all, also searched google to see if anyone had
> written about this. Any help or direction on where to find the answer
> would be much appreciated!
>
> This is in a rails project, but I think the answer lies in Ruby's
> string methods.
> I've got an object that has a list of materials attached to it, so in
> my model I grab all the materials and convert them into a string -
> like this:
>
> self.materials.collect{ |x| x.material.strip.humanize.split.map{ |w|
> w.capitalize}.join(' ')}.join(', ')
>
> So I break apart each material by word, capitalize it, put that
> material string back together and then join all the materials by
> commas.
>
> This works great, except that some of the materials are books with the
> titles surrounded by quotes -
> Book "the Snowy Day," By Ezra Jack Keats
>
> So my code above works, except for the first letter after the initial
> "
> Is there a method to deal with this, so that it correctly capitalizes
> the first letter after the " if there is a quote? I've tried a lot of
> different things, but nothing is giving me the results I need.
>
> Anyone have any ideas?
> Thanks!


Dear Jon,

try this:

text='Book "the Snowy Day," By Ezra Jack Keats'
while /\"([a-z])/.match(text)
ref=/\"([a-z])/.match(text)
text.gsub!(ref[0],'"' + ref[1].capitalize)
end
p text

Best regards,

Axel

--
Psssst! Schon vom neuen GMX MultiMessenger gehrt?
Der kann`s mit allen: http://www.gmx.net/de/go/multimessenger

 
Reply With Quote
 
jonsteenbergen@gmail.com
Guest
Posts: n/a
 
      01-25-2008
Thanks for the help Axel and Alex, I ended up finding out that the
inflector method titleize in rails
did everything I needed it to do.

 
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
problem in running a basic code in python 3.3.0 that includes HTML file Satabdi Mukherjee Python 1 04-04-2013 07:48 PM
Script for finding words of any size that do NOT contain vowels withacute diacritic marks? nwaits Python 10 10-17-2012 06:33 PM
Re: Problem: neither urllib2.quote nor urllib.quote encode theunicode strings arguments Jerry Hill Python 0 10-03-2008 11:37 PM
not dust marks - water marks! pshaw@emmet.com Digital Photography 0 04-14-2005 05:53 AM
XP - requires quote marks typed twice Patrick Dunford NZ Computing 23 07-21-2004 06:45 AM



Advertisments