More better way to split off blank lines

Discussion in 'Ruby' started by trans. (T. Onoma), Oct 17, 2004.

  1. In the following code, write_stack is an array composed of special marker
    objects and strings, and I've extended String with the #blank? method. I want
    to split any space which includes a newline from the front and back of the
    strings and make them their own elements in the array. Note: as the last line
    indicates, I don't necessarily need the new_stack, if it can be done just by
    modifying the write_stack instead.


    # split off blank lines
    new_stack = []
    write_stack.each_index{ |i|
    # if string and not blank
    if write_stack.respond_to?:)blank?) && ! write_stack.blank?
    md = /^(\s*\n\s*)(\S.*\S)(\s*\n\s*)$/.match(write_stack)
    if md
    new_stack << md[1] if md[1].length > 0
    new_stack << md[2]
    new_stack << md[3] if md[3].length > 0
    else
    new_stack << write_stack
    end
    else
    new_stack << write_stack
    end
    }
    write_stack = new_stack


    Is there a nicer way to do this?

    Thanks,
    T.
    trans. (T. Onoma), Oct 17, 2004
    #1
    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 Burgy

    split on blank lines

    Jan Burgy, Dec 1, 2003, in forum: Python
    Replies:
    4
    Views:
    1,040
    Jan Burgy
    Dec 2, 2003
  2. Xiao Jianfeng
    Replies:
    23
    Views:
    2,461
    Fredrik Lundh
    Nov 23, 2005
  3. tmallen
    Replies:
    19
    Views:
    541
    Miles
    Nov 5, 2008
  4. Replies:
    4
    Views:
    97
    Fabian Pilkowski
    Jul 19, 2005
  5. Cah Sableng
    Replies:
    0
    Views:
    231
    Cah Sableng
    Apr 23, 2007
Loading...

Share This Page