Created
March 27, 2019 07:19
-
-
Save 3014zhangshuo/98edcf8e6ec1d6bf0dd8ee72f784bf8d to your computer and use it in GitHub Desktop.
Ruby Array.new
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
collection = Array.new(3, 'abc') | |
collection[0] << 'd' | |
# all collection item changed, because item value is references | |
collection # => ['abcd', 'abcd', 'abcd'] | |
collection = Array.new(3) { 'abc' } | |
collection[0] << 'd' | |
# pass a block, block executed 3 times, each time "abc" is new string not references | |
collection # => ['abcd', 'abc', 'abc'] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment