Sha256: ddf3eaf2e16beb8b7b8b4beb8d683639746c514156965ff8c6c1b13ff98861cb

Contents?: true

Size: 1.11 KB

Versions: 1

Compression:

Stored size: 1.11 KB

Contents

require "active_support/core_ext/string/access"

class String

  remove_method :last

  # Returns the last +limit+ characters from the end of the String.
  #
  # This method replaces Active Support's +String#last+.  However, it
  # returns an empty string when given a negative +limit+, whereas
  # Active Support 6.0 and previous remove +limit.abs+ characters from
  # the beginning of the String.  Returning an empty string is more
  # intuitive behavior if +last+ is interpreted as "keep up to +limit+
  # characters."  (At most, a negative +limit+ should *keep* +limit.abs+
  # characters from the beginning of the String, instead of *remove*
  # that many characters, but returning an empty string is a
  # conservative compromise.)  This method is also faster than Active
  # Support's implementation.
  #
  # @example
  #   "abcdef".last(0)   # == ""
  #   "abcdef".last(3)   # == "def"
  #   "abcdef".last(6)   # == "abcdef"
  #   "abcdef".last(7)   # == "abcdef"
  #   "abcdef".last(-1)  # == ""
  #
  # @param limit [Integer]
  # @return [String]
  def last(limit = 1)
    self[[length - limit, 0].max, limit] || ""
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
casual_support-3.0.2 lib/casual_support/string/last.rb