Skip to content

Instantly share code, notes, and snippets.

@Marahin
Created October 24, 2018 11:10
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 Marahin/069586d4fd809aae99e9967c11bbc04c to your computer and use it in GitHub Desktop.
Save Marahin/069586d4fd809aae99e9967c11bbc04c to your computer and use it in GitHub Desktop.
Are fridge contents frozen or not?
arr = ["john", "alice", "philip"]
=> ["john", "alice", "philip"]
arr.map!(&:upcase) # works, array not frozen
=> ["JOHN", "ALICE", "PHILIP"]
arr
=> ["JOHN", "ALICE", "PHILIP"]
arr.freeze
=> ["JOHN", "ALICE", "PHILIP"]
arr.map!(&:downcase) # doesnt work, array is frozen
FrozenError: can't modify frozen Array
from (pry):5:in `map!'
arr.map(&:downcase!) # works, array is frozen, but particular objects inside are not
=> ["john", "alice", "philip"]
arr
=> ["john", "alice", "philip"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment