,elegant

Discussion in 'Ruby' started by Leslie Viljoen, Dec 13, 2005.

  1. Hiya!

    This function adds commas to a string after every three characters
    so a number would be easier to read. It's kinda ugly though, and I can't
    help thinking there'd be a elegant Why-esque one or two liner to do
    this kind of thing - anyone care to improve it?


    def add_commas(str)
    return nil if str.class != String

    ret = ""
    len = str.length
    i = len - 1
    while i >= 0
    ret = str.chr + ret
    if ((len-i) % 3) == 0
    ret = "," + ret
    end
    i -= 1
    end
    if ret[0].chr == ","
    ret = ret[1..-1]
    end
    ret
    end
     
    Leslie Viljoen, Dec 13, 2005
    #1
    1. Advertising

  2. On Dec 13, 2005, at 11:24 AM, Leslie Viljoen wrote:

    > Hiya!
    >
    > This function adds commas to a string after every three characters
    > so a number would be easier to read. It's kinda ugly though, and I
    > can't
    > help thinking there'd be a elegant Why-esque one or two liner to do
    > this kind of thing - anyone care to improve it?


    Does this help?

    http://rubyurl.com/EOi

    James Edward Gray II
     
    James Edward Gray II, Dec 13, 2005
    #2
    1. Advertising

  3. James Edward Gray II wrote:

    > On Dec 13, 2005, at 11:24 AM, Leslie Viljoen wrote:
    >
    >> Hiya!
    >>
    >> This function adds commas to a string after every three characters
    >> so a number would be easier to read. It's kinda ugly though, and I
    >> can't
    >> help thinking there'd be a elegant Why-esque one or two liner to do
    >> this kind of thing - anyone care to improve it?

    >
    >
    > Does this help?
    >
    > http://rubyurl.com/EOi
    >
    > James Edward Gray II
    >

    I knew it! This kind of thing makes me crazy! I used to think I was a
    good programmer!
     
    Leslie Viljoen, Dec 13, 2005
    #3
  4. Leslie Viljoen

    Guest

    On Wed, 14 Dec 2005, Leslie Viljoen wrote:

    > Hiya!
    >
    > This function adds commas to a string after every three characters
    > so a number would be easier to read. It's kinda ugly though, and I can't
    > help thinking there'd be a elegant Why-esque one or two liner to do
    > this kind of thing - anyone care to improve it?
    >
    >
    > def add_commas(str)
    > return nil if str.class != String
    >
    > ret = ""
    > len = str.length
    > i = len - 1
    > while i >= 0
    > ret = str.chr + ret
    > if ((len-i) % 3) == 0
    > ret = "," + ret
    > end
    > i -= 1
    > end
    > if ret[0].chr == ","
    > ret = ret[1..-1]
    > end
    > ret
    > end


    harp:~ > cat a.rb
    def commafy s
    s.to_s.reverse.scan(%r/.{1,3}/).join(',').reverse
    end

    p(commafy(''))
    p(commafy('4'))
    p(commafy('42'))
    p(commafy('422'))
    p(commafy('4242'))
    p(commafy('42424'))
    p(commafy('424242'))
    p(commafy('4242424'))

    harp:~ > ruby a.rb
    ""
    "4"
    "42"
    "422"
    "4,242"
    "42,424"
    "424,242"
    "4,242,424"

    if you want you can play with the pattern so the two reverses aren't needed...
    but this is quicker to write ;-)

    -a
    --
    ===============================================================================
    | ara [dot] t [dot] howard [at] noaa [dot] gov
    | all happiness comes from the desire for others to be happy. all misery
    | comes from the desire for oneself to be happy.
    | -- bodhicaryavatara
    ===============================================================================
     
    , Dec 13, 2005
    #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. Jan De Ceuster
    Replies:
    5
    Views:
    4,848
    Thomas Stanka
    Jan 13, 2005
  2. Jay Balapa

    Elegant way of returning FieldNames?

    Jay Balapa, Aug 7, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    337
    John Timney \(Microsoft MVP\)
    Aug 7, 2003
  3. =?Utf-8?B?QWxleCBNYWdoZW4=?=

    More Elegant Column Widths in ASP:Table Objects?

    =?Utf-8?B?QWxleCBNYWdoZW4=?=, May 14, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    560
    Jason DeFontes
    May 14, 2004
  4. Braky Wacky
    Replies:
    8
    Views:
    701
    spalding
    Jul 15, 2004
  5. Vladimir

    Elegant algorithm.

    Vladimir, Jul 31, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    395
    Vladimir
    Jul 31, 2004
Loading...

Share This Page