DRY up consecutive gsub's - How to

Discussion in 'Ruby' started by RichardOnRails, Apr 10, 2009.

  1. Hi,

    I have a csv file suitable for an Excel display. The file has column-
    names on certain row.

    In order to process subsequent rows I want to refer to their columns
    symbolically using the column-names. That requires the column-names to
    be cleaned up in order to create valid Ruby symbols.

    The following worked fine:
    if col_name
    col_name.gsub!(/[\s]/, "")
    col_name.gsub!(/[&]/, "And")
    col_name.gsub!(/[#]/, "Number")
    end

    Each of the following one-liners failed:
    col_name.gsub!(/[\s]/, ""). gsub!(/[&]/, "And").gsub!(/[#]/,
    "Number") if col_name
    col_name.gsub(/[\s]/, ""). gsub(/[&]/, "And").gsub(/[#]/, "Number")
    if col_name

    Can either of the one-liners be made to work?

    The code is show in a larger (easier to read) context at http://www.pastie.org/442456

    Thanks in Advance,
    Richard




    ..
    RichardOnRails, Apr 10, 2009
    #1
    1. Advertising

  2. RichardOnRails

    Phlip Guest

    RichardOnRails wrote:

    > Each of the following one-liners failed:
    > col_name.gsub!(/[\s]/, ""). gsub!(/[&]/, "And").gsub!(/[#]/,
    > "Number") if col_name


    col_name = col_name.to_s.gsub(/[\s]/, ""). gsub(/[&]/, "And").gsub(/[#]/,
    "Number")

    If you don't mind col_name shifting from a nil to a blank.

    Also, the [] brackets add no value - both [\s] and \s are a one-width match.
    Phlip, Apr 10, 2009
    #2
    1. Advertising

  3. RichardOnRails

    Robert Dober Guest

    On Fri, Apr 10, 2009 at 3:30 AM, RichardOnRails
    <> wrote:
    > Hi,
    >
    > I have a csv file suitable for an Excel display. =A0The file has column-
    > names on certain row.
    >
    > In order to process subsequent rows I want to refer to their columns
    > symbolically using the column-names. That requires the column-names to
    > be cleaned up in order to create valid Ruby symbols.
    >
    > The following worked fine:
    > =A0 =A0 =A0 =A0if col_name
    > =A0 =A0 =A0 =A0 =A0col_name.gsub!(/[\s]/, "")
    > =A0 =A0 =A0 =A0 =A0col_name.gsub!(/[&]/, "And")
    > =A0 =A0 =A0 =A0 =A0col_name.gsub!(/[#]/, "Number")
    > =A0 =A0 =A0 =A0end
    >
    > Each of the following one-liners failed:
    > =A0 =A0 =A0 =A0 col_name.gsub!(/[\s]/, ""). gsub!(/[&]/, "And").gsub!(/[#=

    ]/,
    > "Number") if col_name
    > =A0 =A0 =A0 =A0 col_name.gsub(/[\s]/, ""). gsub(/[&]/, "And").gsub(/[#]/,=

    "Number")
    > if col_name
    >
    > Can either of the one-liners be made to work?
    >
    > The code is show in a larger (easier to read) context at http://www.pasti=

    e.org/442456
    >
    > Thanks in Advance,
    > Richard



    Here is a oneliner
    col.gsub!(/[\s&#]/){ |x| x =3D=3D "#" ? "Number" : x =3D=3D "&" ? "And" : "=
    " }
    just to show that your code is much better.
    Why do you dislike it, it is readable and simple.
    If you are worried about changes, you might potentially factor that out

    [ /\s/, "", "&", "And", "#', "Number"].each_slice( 2 ) do | reg, rep |
    col.gsub! reg, rep
    end

    HTH
    Robert
    >
    >
    >
    > .
    >
    >
    >




    --=20
    If you want to build a ship, don=92t herd people together to collect
    wood and don=92t assign them tasks and work, but rather teach them to
    long for the endless immensity of the sea.
    -- Antoine de Saint-Exupery
    Robert Dober, Apr 10, 2009
    #3
  4. On Apr 9, 9:25 pm, RichardOnRails
    <> wrote:
    > Hi,
    >
    > I have a csv file suitable for an Excel display.  The file has column-
    > names on certain row.
    >
    > In order to process subsequent rows I want to refer to their columns
    > symbolically using the column-names. That requires the column-names to
    > be cleaned up in order to create valid Ruby symbols.
    >
    > The following worked fine:
    >         if col_name
    >           col_name.gsub!(/[\s]/, "")
    >           col_name.gsub!(/[&]/, "And")
    >           col_name.gsub!(/[#]/, "Number")
    >         end
    >
    > Each of the following one-liners failed:
    >          col_name.gsub!(/[\s]/, ""). gsub!(/[&]/, "And").gsub!(/[#]/,
    > "Number") if col_name
    >          col_name.gsub(/[\s]/, ""). gsub(/[&]/, "And").gsub(/[#]/, "Number")
    > if col_name
    >
    > Can either of the one-liners be made to work?
    >
    > The code is show in a larger (easier to read) context athttp://www.pastie..org/442456
    >
    > Thanks in Advance,
    > Richard
    >
    > .


    Hi Philip and Robert,

    Thank you both for great responses.

    First off, I still wanted to know what was the flaw in my one-liner;
    I found two:
    1. I needed carefully placed parenthesis
    2. The parenthesized version failed because I just discovered) gsub!
    returns nil when no change is made. Bad idea for my purpose; no doubt
    Matz saw a good reason for it.

    Bob, your version worked fine and fulfills my goal
    Ditto for your version, Robert.
    For both, I appended "if if col_name" to handle unnamed columns.

    With two great choices, my aesthetics lead me to Robert's version
    because:
    1. I like the x.gsub! ... better than x = x.gsub ...
    2. The single gsub! with the choices laid out in order packs expanding
    it for additional cases with the least typing.

    Again, thank you both very much for your help.

    Best wishes,
    Richard

    Most of all, I like one-liners much better than 5-liners for
    readability.
    RichardOnRails, Apr 10, 2009
    #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. yoda
    Replies:
    2
    Views:
    270
  2. Todd
    Replies:
    0
    Views:
    309
  3. R. Bernstein

    How do I DRY the following code?

    R. Bernstein, Dec 30, 2008, in forum: Python
    Replies:
    4
    Views:
    211
    R. Bernstein
    Dec 30, 2008
  4. aurelianito

    gsub and gsub! are inconsistent

    aurelianito, Nov 8, 2005, in forum: Ruby
    Replies:
    9
    Views:
    149
    Robert Klemme
    Nov 9, 2005
  5. Josselin

    DRY gsub...

    Josselin, Jan 12, 2007, in forum: Ruby
    Replies:
    31
    Views:
    308
    Gregory Seidman
    Jan 14, 2007
Loading...

Share This Page