File: unindent.rb

package info (click to toggle)
ruby-cucumber-core 1.5.0-3
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 580 kB
  • sloc: ruby: 5,763; makefile: 2
file content (14 lines) | stat: -rw-r--r-- 436 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# Copyright © 2009 Martin Aumont (mynyml)
# Licence Expat
# https://github.com/mynyml/unindent/blob/master/lib/unindent.rb
# https://github.com/mynyml/unindent/blob/master/LICENSE

class String
  def unindent
    indent = self.split("\n").select {|line| !line.strip.empty? }.map {|line| line.index(/[^\s]/) }.compact.min || 0
    self.gsub(/^[[:blank:]]{#{indent}}/, '')
  end
  def unindent!
    self.replace(self.unindent)
  end
end