Sha256: 88510ff71abc54a84f50bd82c371927a80325e7f832a3cd9fcacfbf5a3bd7ef7

Contents?: true

Size: 433 Bytes

Versions: 3

Compression:

Stored size: 433 Bytes

Contents

class String
  # Returns last _n_ characters.
  #
  #   require 'facet/string/last_char'
  #
  #   "Hello World".last_char(3)  #=> "rld"
  #
  def last_char(n=1)
    n = n.to_i
    return self if n > size
    slice(-n, n) #self[-n..-1]
  end
end


# __TEST__

if __FILE__ == $0
  require 'test/unit'
  class TestCase < Test::Unit::TestCase
    def test_first
      assert_equal( "rld", "Hello World".last_char(3) )
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
facets-0.7.0 lib/facet/string/last_char.rb
facets-0.7.1 lib/facet/string/last_char.rb
facets-0.7.2 lib/facet/string/last_char.rb