remove overall indentation preserving reletive indentation

Discussion in 'Ruby' started by Jesse B., Mar 27, 2010.

  1. Jesse B.

    Jesse B. Guest

    I am trying to find a solution to remove the overall indentation of a
    block of text while preserving the relative indentation of each line,
    for example

    line 1
    line 2

    (2 spaces then 4 spaces)
    should become

    line 1
    line 2

    (0 spaces then two spaces)

    so that the two space between lines relative indentation is preserved,
    yet the first two spaces overall indentation goes away.

    what I have tried so far is string .strip, which seems to remove all
    leading space of the first line only

    content =
    " line 1
    line2"
    output = content.strip
    print output

    produces

    line 1
    line 2

    0 spaces then 4.

    whereas Im going for

    a result of 0 then 2 from starting with 2 then 4.

    I assume the solution would probably involve getting the number of
    spaces for the first line and removing that number of spaces from each
    line in the block?

    thanks in advance for your help.
    --
    Posted via http://www.ruby-forum.com/.
    Jesse B., Mar 27, 2010
    #1
    1. Advertising

  2. Hello Jesse,

    > I assume the solution would probably involve getting the number of
    > spaces for the first line and removing that number of spaces from each
    > line in the block?


    You are also assuming your text is consistently indented from the
    first line, that is you don't have

    ....line 1
    ..line 2
    ....line 3

    for example. A better way would be (I think) to check first each line
    to search for the minimum number of spaces and then remove that number
    of spaces from each line (assuming also that you have normal spaces
    and no tabulations for example). One way would be

    string =3D " line1\n line2\n line3"
    puts string
    arr =3D string.split(/\n/)
    nb =3D arr.collect {|line| line =3D~ /^( *)/ ; $1.length}.min
    new_string =3D arr.collect {|line| line.sub(/ {#{nb}}/,'')}.join("\n")
    puts new_string

    Cheers,

    --=20
    JJ Fleck
    PCSI1 Lyc=E9e Kl=E9ber
    Jean-Julien Fleck, Mar 27, 2010
    #2
    1. Advertising

  3. Jesse B.

    Josh Cheek Guest

    [Note: parts of this message were removed to make it a legal post.]

    On Sat, Mar 27, 2010 at 10:33 AM, Jesse B. <> wrote:

    > I am trying to find a solution to remove the overall indentation of a
    > block of text while preserving the relative indentation of each line,
    > for example
    >
    > line 1
    > line 2
    >
    > (2 spaces then 4 spaces)
    > should become
    >
    > line 1
    > line 2
    >
    > (0 spaces then two spaces)
    >
    > so that the two space between lines relative indentation is preserved,
    > yet the first two spaces overall indentation goes away.
    >
    > what I have tried so far is string .strip, which seems to remove all
    > leading space of the first line only
    >
    > content =
    > " line 1
    > line2"
    > output = content.strip
    > print output
    >
    > produces
    >
    > line 1
    > line 2
    >
    > 0 spaces then 4.
    >
    > whereas Im going for
    >
    > a result of 0 then 2 from starting with 2 then 4.
    >
    > I assume the solution would probably involve getting the number of
    > spaces for the first line and removing that number of spaces from each
    > line in the block?
    >
    > thanks in advance for your help.
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >


    Perhaps

    def relative_indented(str)
    leading_whitespace = str[/\A\s*/]
    str.gsub /^#{leading_whitespace}/ , String.new
    end


    puts relative_indented <<END_OF_TEXT
    Two Spaces
    Four Spaces
    Six Spaces
    Four Spaces
    Two Spaces
    END_OF_TEXT

    A few things, though. Everything is relative to your initial indenting, so
    if you have a line later with less indenting, then it will just go to zero
    (will not be relative, because it would need negative indenting). You could
    get around this by checking each line to find the one with the largest
    indenting, and make them relative to that, whereas I have just checked it
    against the first line in the string. Also, mixing spaces with tabs will
    mess it up, unless you are consistent with your use across every line.
    Josh Cheek, Mar 27, 2010
    #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. Paul Ritchie

    Best overall reporting package?

    Paul Ritchie, Aug 14, 2003, in forum: ASP .Net
    Replies:
    4
    Views:
    2,734
    dotnetdev18
    Oct 27, 2008
  2. Steven Bethard
    Replies:
    11
    Views:
    1,000
    Alex Martelli
    Feb 7, 2005
  3. TTroy
    Replies:
    16
    Views:
    769
    Peter Nilsson
    Jan 31, 2005
  4. Replies:
    4
    Views:
    492
  5. Robster
    Replies:
    1
    Views:
    84
    Lasse Reichstein Nielsen
    Feb 8, 2004
Loading...

Share This Page