You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: nchar.rb
    path of file: ./repos/facets/work/OLD_COMPARE/facets/string
    
    the code of the file until where you have to start completion: 
class String

  # Retrns _n_ characters of the string. If _n_ is positi
    
  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