Skip to content

Instantly share code, notes, and snippets.

@veganstraightedge
Last active April 26, 2017 03:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save veganstraightedge/4ea19550937af96c1c9b70de856e042f to your computer and use it in GitHub Desktop.
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.
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
@veganstraightedge
Copy link
Author

exercise.rb:5: Can't change the value of self
    self = self[1..-1]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment