Skip to content

Instantly share code, notes, and snippets.

@3014zhangshuo
Created March 27, 2019 07:19
Show Gist options
  • Save 3014zhangshuo/98edcf8e6ec1d6bf0dd8ee72f784bf8d to your computer and use it in GitHub Desktop.
Save 3014zhangshuo/98edcf8e6ec1d6bf0dd8ee72f784bf8d to your computer and use it in GitHub Desktop.
Ruby Array.new
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