Sha256: a5d5c3f41414c24e196bfff932be23a1546ff73c8164b1b0a3bb058f3800d48b

Contents?: true

Size: 659 Bytes

Versions: 1

Compression:

Stored size: 659 Bytes

Contents

class String

  # This replaces Active Support's +String#first+, but it returns an
  # empty string when given a negative +limit+ argument, whereas Active
  # Support's +String#first+ removes +limit.abs+ characters from the
  # end of the string.  Returning an empty string makes more sense if
  # you interpret +first+ as "keep upto +limit+ characters."  At most, a
  # negative +limit+ should *keep* that many characters from the end of
  # the string, rather than *remove* that many characters (but returning
  # an empty string is a good conservative choice).
  #
  # This method is also faster.
  def first(limit = 1)
    self[0, limit] || ''
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

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