Ruby Array Initialization [duplicate]
Arguments are always evaluated prior to a method call whereas a block is evaluated only during the method call at a timing controlled by the method (if it is ever evaluated).
In your first example, the argument "abc"
is evaluated once before the method new
is called. The evaluated object is passed to the method new
. The exact same object is used in the all three elements of the created array. Modifying one means modifying all of them.
In your second example, the block {"abc"}
is evaluated each time a new element is generated for the array. The three elements in the created array are different objects.