Skip to content

Instantly share code, notes, and snippets.

@masterkain
Created July 31, 2011 22:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save masterkain/1117299 to your computer and use it in GitHub Desktop.
Save masterkain/1117299 to your computer and use it in GitHub Desktop.
splice in ruby
class Array
def splice(start, len, *replace)
self[start, len] = replace
self
end
end
ruby-1.8.7-p352 :008 > initial_array = [:a, :c, :h, :g, :t, :m]
=> [:a, :c, :h, :g, :t, :m]
ruby-1.8.7-p352 :009 > initial_array.splice(2, 2, :test)
=> [:a, :c, :test, :t, :m]
ruby-1.8.7-p352 :010 > initial_array.splice(2, 2, :test, :test2)
=> [:a, :c, :test, :test2, :m]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment