diff options
author | Xavier Noria <fxn@hashref.com> | 2012-08-07 16:55:28 +0200 |
---|---|---|
committer | Xavier Noria <fxn@hashref.com> | 2012-08-07 16:57:28 +0200 |
commit | 2f58795e783150f2e1b1f6c64e305703f0061129 (patch) | |
tree | caa757a14597b9d6a65104bc5d85c009f9160820 /activesupport/lib | |
parent | 9cd1f697f1b4dded0f18e13c7ae12c7bd2dab1c0 (diff) | |
download | rails-2f58795e783150f2e1b1f6c64e305703f0061129.tar.gz rails-2f58795e783150f2e1b1f6c64e305703f0061129.tar.bz2 rails-2f58795e783150f2e1b1f6c64e305703f0061129.zip |
defines String#indent [closes #7263] [Xavier Noria & Ace Suares]
Diffstat (limited to 'activesupport/lib')
-rw-r--r-- | activesupport/lib/active_support/core_ext/string.rb | 1 | ||||
-rw-r--r-- | activesupport/lib/active_support/core_ext/string/indent.rb | 43 |
2 files changed, 44 insertions, 0 deletions
diff --git a/activesupport/lib/active_support/core_ext/string.rb b/activesupport/lib/active_support/core_ext/string.rb index fb36262528..ad864765a3 100644 --- a/activesupport/lib/active_support/core_ext/string.rb +++ b/activesupport/lib/active_support/core_ext/string.rb @@ -10,3 +10,4 @@ require 'active_support/core_ext/string/output_safety' require 'active_support/core_ext/string/exclude' require 'active_support/core_ext/string/strip' require 'active_support/core_ext/string/inquiry' +require 'active_support/core_ext/string/indent' diff --git a/activesupport/lib/active_support/core_ext/string/indent.rb b/activesupport/lib/active_support/core_ext/string/indent.rb new file mode 100644 index 0000000000..afc3032272 --- /dev/null +++ b/activesupport/lib/active_support/core_ext/string/indent.rb @@ -0,0 +1,43 @@ +class String + # Same as +indent+, except it indents the receiver in-place. + # + # Returns the indented string, or +nil+ if there was nothing to indent. + def indent!(amount, indent_string=nil, indent_empty_lines=false) + indent_string = indent_string || self[/^[ \t]/] || ' ' + re = indent_empty_lines ? /^/ : /^(?!$)/ + gsub!(re, indent_string * amount) + end + + # Indents the lines in the receiver: + # + # <<EOS.indent(2) + # def some_method + # some_code + # end + # EOS + # # => + # def some_method + # some_code + # end + # + # The second argument, +indent_string+, specifies which indent string to + # use. The default is +nil+, which tells the method to make a guess by + # peeking at the first indented line, and fallback to a space if there is + # none. + # + # " foo".indent(2) # => " foo" + # "foo\n\t\tbar".indent(2) # => "\t\tfoo\n\t\t\t\tbar" + # "foo".indent(2, "\t") # => "\t\tfoo" + # + # While +indent_string+ is tipically one space or tab, it may be any string. + # + # The third argument, +indent_empty_lines+, is a flag that says whether + # empty lines should be indented. Default is false. + # + # "foo\n\nbar".indent(2) # => " foo\n\n bar" + # "foo\n\nbar".indent(2, nil, true) # => " foo\n \n bar" + # + def indent(amount, indent_string=nil, indent_empty_lines=false) + dup.tap {|_| _.indent!(amount, indent_string, indent_empty_lines)} + end +end |