Sha256: bd82cf5c76f0e3e74bcb46eb741b444ce1c8a7bbbfdf6b977af83e6412cb7566

Contents?: true

Size: 1.1 KB

Versions: 1

Compression:

Stored size: 1.1 KB

Contents

require "active_support/core_ext/string/access"

class String

  remove_method :first

  # Returns the first +limit+ characters from the beginning of the
  # String.
  #
  # This method replaces Active Support's +String#first+.  However, it
  # returns an empty string when given a negative +limit+, whereas
  # Active Support 6.0 and previous remove +limit.abs+ characters from
  # the end of the String.  Returning an empty string is more intuitive
  # behavior if +first+ is interpreted as "keep up to +limit+
  # characters."  (At most, a negative +limit+ should *keep* +limit.abs+
  # characters from the end 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".first(0)   # == ""
  #   "abcdef".first(3)   # == "abc"
  #   "abcdef".first(6)   # == "abcdef"
  #   "abcdef".first(7)   # == "abcdef"
  #   "abcdef".first(-1)  # == ""
  #
  # @param limit [Integer]
  # @return [String]
  def first(limit = 1)
    self[0, limit] || ""
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

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