Created
July 31, 2011 22:36
-
-
Save masterkain/1117299 to your computer and use it in GitHub Desktop.
splice in ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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