Sha256: ccf49e72154fff8cd580b954f4cb82fd0844eb90d95ba8a075584cd4d26eb16c

Contents?: true

Size: 1.42 KB

Versions: 214

Compression:

Stored size: 1.42 KB

Contents

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 typically 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

Version data entries

214 entries across 206 versions & 23 rubygems

Version Path
files.com-1.0.55 docs/vendor/bundle/ruby/2.5.0/gems/activesupport-5.0.7.1/lib/active_support/core_ext/string/indent.rb
activesupport-4.2.11.3 lib/active_support/core_ext/string/indent.rb
activesupport-4.2.11.2 lib/active_support/core_ext/string/indent.rb
motion-support-1.2.1 motion/core_ext/string/indent.rb
motion-support-1.1.1 motion/core_ext/string/indent.rb
cocoapods-dependency-html-0.0.2 vendor/bundle/gems/activesupport-4.2.11.1/lib/active_support/core_ext/string/indent.rb
cocoapods-dependency-html-0.0.1 vendor/bundle/gems/activesupport-4.2.11.1/lib/active_support/core_ext/string/indent.rb
activesupport-5.0.7.2 lib/active_support/core_ext/string/indent.rb
activesupport-4.2.11.1 lib/active_support/core_ext/string/indent.rb
activesupport-5.0.7.1 lib/active_support/core_ext/string/indent.rb
activesupport-4.2.11 lib/active_support/core_ext/string/indent.rb
motion-support-1.2.0 motion/core_ext/string/indent.rb
activesupport-5.0.7 lib/active_support/core_ext/string/indent.rb
activesupport-4.2.10 lib/active_support/core_ext/string/indent.rb
activesupport-4.2.10.rc1 lib/active_support/core_ext/string/indent.rb
activesupport-5.0.6 lib/active_support/core_ext/string/indent.rb
activesupport-5.0.6.rc1 lib/active_support/core_ext/string/indent.rb
activesupport-5.0.5 lib/active_support/core_ext/string/indent.rb
activesupport-5.0.5.rc2 lib/active_support/core_ext/string/indent.rb
activesupport-5.0.5.rc1 lib/active_support/core_ext/string/indent.rb