strings that contain quote marks

Discussion in 'Ruby' started by jonsteenbergen@gmail.com, Jan 24, 2008.

  1. Guest

    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!
    , Jan 24, 2008
    #1
    1. Advertising

  2. Alex LeDonne Guest

    On Jan 24, 2008 10:59 AM,
    <> 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
    Alex LeDonne, Jan 24, 2008
    #2
    1. Advertising

  3. Axel Etzold Guest

    -------- Original-Nachricht --------
    > Datum: Fri, 25 Jan 2008 00:59:58 +0900
    > Von: "" <>
    > An:
    > 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 gehört?
    Der kann`s mit allen: http://www.gmx.net/de/go/multimessenger
    Axel Etzold, Jan 24, 2008
    #3
  4. Guest

    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.
    , Jan 25, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jerry Hill
    Replies:
    0
    Views:
    474
    Jerry Hill
    Oct 4, 2008
  2. Roger Pack
    Replies:
    3
    Views:
    147
    Roger Pack
    Sep 28, 2010
  3. OC

    Too Many Quote Marks

    OC, May 21, 2004, in forum: Javascript
    Replies:
    2
    Views:
    86
  4. Jason Carlton
    Replies:
    11
    Views:
    233
    Dr J R Stockton
    Dec 8, 2009
  5. nwaits
    Replies:
    10
    Views:
    211
Loading...

Share This Page