Skip to content

Commit

Permalink
Don't let blank lines force indent to zero
Browse files Browse the repository at this point in the history
  • Loading branch information
djsun committed Apr 20, 2010
1 parent d8260ef commit 53bf0b5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
14 changes: 12 additions & 2 deletions lib/unindentable.rb
Expand Up @@ -10,14 +10,24 @@ def unindent(string)
# Returns the indent level of a string (with multiple lines)
def find_minimum_indent(string)
indents = []
string.each_line { |l| indents << l.index(/[^ ]/) }
string.each_line do |line|
unless line == "\n"
indents << line.index(/[^ ]/)
end
end
indents.min
end

# Trims the leftmost n characters from each line of a string
def left_trim(string, n)
new_string = ""
string.each_line { |l| new_string << l.slice(n, l.length) }
string.each_line do |line|
new_string << if line == "\n"
"\n"
else
line.slice(n, line.length)
end
end
new_string
end

Expand Down
9 changes: 9 additions & 0 deletions spec/unindentable_spec.rb
Expand Up @@ -63,4 +63,13 @@
x.should == "The first line\n The second line\n \nThe fourth line\n"
end

it "should not let blank lines break the indent" do
x = unindent <<-BLOCK
The first line
The third line
BLOCK
x.should == "The first line\n\nThe third line\n"
end

end

0 comments on commit 53bf0b5

Please sign in to comment.