Sha256: 5c7a0a102842b7dc991e2c802d88db2e13303789b03aa5841467191ad4bb624d

Contents?: true

Size: 596 Bytes

Versions: 10

Compression:

Stored size: 596 Bytes

Contents

class String

  # This is basically the same as #store,
  # but it acts like slice! when given only
  # one argument.
  #
  # Essentlay #slice, but writes rather than
  # reads.
  #
  #   a = "HELLO"
  #   a.splice("X", 1)
  #   a                #=> "HXLLO"
  #
  #   a = "HELLO"
  #   a.splice(1)    #=> "E"
  #   a              #=> "HLLO"
  #
  # CREDIT: Trans

  def splice(idx, sub=nil)
    if sub
      store(idx, sub)
    else
      case idx
      when Range
        slice!(idx)
      else
        slice!(idx,1)
      end
    end
  end

  # Alias for []=.
  alias_method :store, :[]=

end

Version data entries

10 entries across 10 versions & 1 rubygems

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