can it be shorter?

Discussion in 'Ruby' started by WuyaSea Operator, Dec 11, 2006.

  1. doing formating, i need to break a long line of words to roughly 80
    chars per line, below is what I got

    def break_line(text)
    return text if text.size < 80

    i = text.index(' ', 79)
    text[0..i] + "\n" + break_line(text[i+1..-1])
    end


    can it be any shorter, more rubyish? Thank you.

    operator
    www.wuyaSea.com
    WuyaSea Operator, Dec 11, 2006
    #1
    1. Advertising

  2. On Monday 11 December 2006 14:20, WuyaSea Operator wrote:
    > doing formating, i need to break a long line of words to roughly 80
    > chars per line, below is what I got
    >
    > def break_line(text)
    > return text if text.size < 80
    >
    > i = text.index(' ', 79)
    > text[0..i] + "\n" + break_line(text[i+1..-1])
    > end
    >
    >
    > can it be any shorter, more rubyish? Thank you.
    >


    It's not perfect, but well... just inviting all the regex-gurus to improve
    it ;)

    ^manveru

    class String
    def linearize(max = 60)
    scan(/.{1,#{max}}(?=\s|$)/)
    end
    end

    "The Lojban alphabet consists of the 26 characters".linearize
    # ["The Lojban alphabet consists of the 26 characters"]

    "The Lojban alphabet consists of the 26 characters".linearize 10
    # ["The Lojban", " alphabet", " consists", " of the 26", "characters"]

    "The Lojban alphabet consists of the 26 characters".linearize 20
    # ["The Lojban alphabet", " consists of the 26", " characters"]

    "The Lojban alphabet consists of the 26 characters".linearize 30
    # ["The Lojban alphabet consists", " of the 26 characters"]

    "The Lojban alphabet consists of the 26 characters".linearize 40
    # ["The Lojban alphabet consists of the 26", " characters"]

    > operator
    > www.wuyaSea.com
    Michael Fellinger, Dec 11, 2006
    #2
    1. Advertising

  3. On Dec 10, 2006, at 11:20 PM, WuyaSea Operator wrote:

    > doing formating, i need to break a long line of words to roughly 80
    > chars per line, below is what I got
    >
    > def break_line(text)
    > return text if text.size < 80
    >
    > i = text.index(' ', 79)
    > text[0..i] + "\n" + break_line(text[i+1..-1])
    > end
    >
    >
    > can it be any shorter, more rubyish? Thank you.


    def break_line(text)
    text.gsub!(/(.{1,80}|\S{81,})(?: +|$\n?)/, "\\1\n")
    end

    James Edward Gray II
    James Edward Gray II, Dec 11, 2006
    #3
    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. chenyu
    Replies:
    1
    Views:
    277
    Bob Gailer
    Nov 12, 2003
  2. Lowell Kirsh

    how can I make this script shorter?

    Lowell Kirsh, Feb 22, 2005, in forum: Python
    Replies:
    7
    Views:
    280
    Christos TZOTZIOY Georgiou
    Feb 25, 2005
  3. trvlDev
    Replies:
    3
    Views:
    263
    trvlDev
    Apr 6, 2007
  4. kj

    Re: can it be shorter?

    kj, Jun 6, 2009, in forum: Python
    Replies:
    5
    Views:
    311
  5. Aaron Brady

    Re: can it be shorter?

    Aaron Brady, Jun 7, 2009, in forum: Python
    Replies:
    7
    Views:
    333
    Carlos Valiente
    Jun 11, 2009
Loading...

Share This Page