Sha256: 2b009e6e748bb3b9993bfb86c62e5aad4c02c2f582b4924d7dfdef503794305b

Contents?: true

Size: 940 Bytes

Versions: 26

Compression:

Stored size: 940 Bytes

Contents

class String

  # Retrns _n_ characters of the string. If _n_ is positive
  # the characters are from the beginning of the string.
  # If _n_ is negative from the end of the string.
  #
  # Alternatively a replacement string can be given, which will
  # replace the _n_ characters.
  #
  def nchar( n, replacement=nil )
    if replacement
      s = self.dup
      n > 0 ? (s[0...n] = replacement) : (s[n..-1] = replacement)
      return s
    else
      n > 0 ? self[0...n] : self[n..-1]
    end
  end

end


#  _____         _
# |_   _|__  ___| |_
#   | |/ _ \/ __| __|
#   | |  __/\__ \ |_
#   |_|\___||___/\__|
#
=begin test

  require 'test/unit'

  class TCString < Test::Unit::TestCase

    def test_nchar
      assert_equal( "abc", "abcxyz".nchar(3) )
      assert_equal( "xyz", "abcxyz".nchar(-3) )
      assert_equal( "HIxyz", "abcxyz".nchar(3, 'HI') )
      assert_equal( "abcHI", "abcxyz".nchar(-3, 'HI') )
    end

  end

=end

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
facets-0.9.0 lib/nano/string/nchar.rb
facets-1.0.0 lib/facet/string/nchar.rb
facets-1.0.3 packages/core/lib/facet/string/nchar.rb
facets-1.2.1 lib/facets/core/string/nchar.rb
facets-1.2.0 lib/facets/core/string/nchar.rb
facets-1.3.0 lib/facets/core/string/nchar.rb
facets-1.1.0 lib/facet/string/nchar.rb
facets-1.3.1 lib/facets/core/string/nchar.rb
facets-1.3.3 lib/facets/core/string/nchar.rb
facets-1.3.2 lib/facets/core/string/nchar.rb
facets-1.4.0 lib/facets/core/string/nchar.rb
facets-1.4.2 lib/facets/core/string/nchar.rb
facets-1.4.1 lib/facets/core/string/nchar.rb
facets-1.4.3 lib/facets/core/string/nchar.rb
facets-1.4.5 lib/facets/core/string/nchar.rb
facets-1.4.4 lib/facets/core/string/nchar.rb
facets-1.7.30 lib/facets/core/string/nchar.rb
facets-1.7.38 lib/facets/core/string/nchar.rb
facets-1.7.0 lib/facets/core/string/nchar.rb
facets-1.7.46 lib/facets/core/string/nchar.rb