Sha256: 4a3118ab2f35f1e41468913c1e72673ae254f558a6d1c32216a318794d763935

Contents?: true

Size: 530 Bytes

Versions: 10

Compression:

Stored size: 530 Bytes

Contents

require 'facets/string/store'

class String

  # String#slice is essentially the same as #store.
  #
  #   a = "HELLO"
  #   a.splice(1, "X")
  #   a                #=> "HXLLO"
  #
  # But it acts like #slice! when given a single argument.
  #
  #   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

end

Version data entries

10 entries across 9 versions & 2 rubygems

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