Sha256: 1ce73f2b5ce8e8171d4d3e29e2910ca137a9ea59efd1c7388dec4a7b63346dd7

Contents?: true

Size: 1.46 KB

Versions: 194

Compression:

Stored size: 1.46 KB

Contents

# frozen_string_literal: true

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

194 entries across 180 versions & 23 rubygems

Version Path
activesupport-7.0.8.6 lib/active_support/core_ext/string/indent.rb
activesupport-6.1.7.10 lib/active_support/core_ext/string/indent.rb
activesupport-6.1.7.9 lib/active_support/core_ext/string/indent.rb
activesupport-7.0.8.5 lib/active_support/core_ext/string/indent.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/activesupport-7.0.8.4/lib/active_support/core_ext/string/indent.rb
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/activesupport-7.0.5.1/lib/active_support/core_ext/string/indent.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/activesupport-7.0.5.1/lib/active_support/core_ext/string/indent.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/activesupport-7.0.5.1/lib/active_support/core_ext/string/indent.rb
activesupport-7.0.8.4 lib/active_support/core_ext/string/indent.rb
activesupport-6.1.7.8 lib/active_support/core_ext/string/indent.rb
activesupport-7.0.8.1 lib/active_support/core_ext/string/indent.rb
activesupport-6.1.7.7 lib/active_support/core_ext/string/indent.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/activesupport-7.0.3.1/lib/active_support/core_ext/string/indent.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/activesupport-7.0.2.3/lib/active_support/core_ext/string/indent.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/activesupport-6.1.6.1/lib/active_support/core_ext/string/indent.rb
activesupport-7.1.2 lib/active_support/core_ext/string/indent.rb
activesupport-7.1.1 lib/active_support/core_ext/string/indent.rb
activesupport-7.1.0 lib/active_support/core_ext/string/indent.rb
activesupport-7.1.0.rc2 lib/active_support/core_ext/string/indent.rb
activesupport-7.1.0.rc1 lib/active_support/core_ext/string/indent.rb