Sha256: 9053374669900366ecb6ac23b3c5b7c82e2948fb1c53305f51eab4348f89acb2

Contents?: true

Size: 757 Bytes

Versions: 10

Compression:

Stored size: 757 Bytes

Contents

class String

  # Returns _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.
  #
  #    str = "this is text"
  #
  #    str.nchar(4)            #=> "this"
  #    str.nchar(-4)           #=> "text"
  #
  # Alternatively a replacement string can be given, which will
  # replace the _n_ characters.
  #
  #    str.nchar(4, 'that')    #=> "that is text"
  #
  # The original string remains unaffected.
  #
  #    str  #=> "this is text"
  #
  def nchar(n, replacement=nil)
    if replacement
      s = self.dup
      n > 0 ? (s[0...n] = replacement) : (s[n..-1] = replacement)
      s
    else
      n > 0 ? self[0...n] : self[n..-1]
    end
  end

end

Version data entries

10 entries across 9 versions & 2 rubygems

Version Path
facets-glimmer-3.2.0 lib/core/facets/string/nchar.rb
facets-3.1.0 lib/core/facets/string/nchar.rb
facets-3.0.0 lib/core/facets/string/nchar.rb
facets-2.9.3 lib/core/facets/string/nchar.rb
facets-2.9.2 lib/core/facets/string/nchar.rb
facets-2.9.2 src/core/facets/string/nchar.rb
facets-2.9.1 lib/core/facets/string/nchar.rb
facets-2.9.0 lib/core/facets/string/nchar.rb
facets-2.9.0.pre.2 lib/core/facets/string/nchar.rb
facets-2.9.0.pre.1 lib/core/facets/string/nchar.rb