Method: String#splice

Defined in:
lib/core/facets/string/splice.rb

#splice(idx, sub = nil) ⇒ Object

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



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/core/facets/string/splice.rb', line 19

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