Sha256: 646b6bce6a3bcf11b33212728d446039e90889d3dbf68c739f77214c002bd963

Contents?: true

Size: 673 Bytes

Versions: 10

Compression:

Stored size: 673 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.
  #
  # Alternatively a replacement string can be given, which will
  # replace the _n_ characters.
  #
  #    str = "this is text"
  #    str.nchar(4)            #=> "this"
  #    str.nchar(4, 'that')    #=> "that"
  #    str                     #=> "that is text"
  #
  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

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
facets-2.8.4 lib/core/facets/string/nchar.rb
facets-2.8.3 lib/core/facets/string/nchar.rb
facets-2.8.2 lib/core/facets/string/nchar.rb
facets-2.8.1 lib/core/facets/string/nchar.rb
facets-2.8.0 lib/core/facets/string/nchar.rb
facets-2.7.0 lib/core/facets/string/nchar.rb
facets-2.6.0 lib/core/facets/string/nchar.rb
facets-2.5.1 lib/core/facets/string/nchar.rb
facets-2.5.0 lib/core/facets/string/nchar.rb
facets-2.5.2 lib/core/facets/string/nchar.rb