Weird Things I Encountered In My Development Journey Explained

Using Shovel Operator in Ruby Hashes

grocery_store = Hash.new([])
grocery_store[:fruits]
=> []
grocery_store[:fruits] << "Apples"
=> ["Apples"]
grocery_store
=> {}
grocery_store[:fruits] = grocery_store[:fruits] << "Oranges"
grocery_store
=> {:fruits=>["Apple", "Oranges"]}
grocery_store[:vegetable] <<= "Onions"
grocery_store
=> {:fruits=>["Apple", "Oranges", "Onions"], :vegetable=>["Apple", "Oranges", "Onions"]}
grocery_store[:vegetable].object_id
=> 200
grocery_store[:fruits].object_id
=> 200
grocery_store = Hash.new{[]}
grocery_store[:fruits].object_id
=> 220
grocery_store[:vegetable].object_id
=> 240

--

--

End to End Product Developer with an immense passion for Rails and React. https://github.com/shanshaji

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store