Last active
April 26, 2017 03:22
-
-
Save veganstraightedge/4ea19550937af96c1c9b70de856e042f to your computer and use it in GitHub Desktop.
As a fun exercise, Morgan and I are implement shift, unshift, push and pop in ruby and js. The return is nbd. The changing the array in place stumped me. Bc I didn’t realize you change self.
This file contains hidden or 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 shift | |
output = self.first | |
self = self[1..-1] | |
output | |
end | |
def unshift(item) | |
[item] + self | |
end | |
def push(item) | |
self << item | |
end | |
def pop | |
self.last | |
end | |
end | |
arr = %w(a b c d e) | |
puts arr.shift.inspect | |
puts arr.inspect |
Author
veganstraightedge
commented
Apr 26, 2017
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment