Skip to content

Instantly share code, notes, and snippets.

@nelantone
Last active January 27, 2021 12:15
Show Gist options
  • Save nelantone/0f6bd2fb850cda9052ce92b22357c397 to your computer and use it in GitHub Desktop.
Save nelantone/0f6bd2fb850cda9052ce92b22357c397 to your computer and use it in GitHub Desktop.
Explanation of variable shadowing in special case

on line 1 we initialize the variable assiging it to a string collection.

on line 4 we call each method on a object assigned to the object collection pointing to a.

on line 5 as we the variable is shadowing each specific objects 'a','b', 'c' will be modified to upcase(if we will try to call a.upcase! out of the block we will raise an exception).

on line 6 we modify again each object, but the array is the same, so we can't modify the array

on line 9 the return value is => ['Ad','Bd','Cd'] because we modify each object inside the collection but not the array the concept of variable shadowing is also applied in case will not be applied we will have a => ['A','d','C']

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