3

I need to know the technical difference between these 2 statements and why it behaves like that:

arr = Array.new(3, "abc")
=> ["abc","abc","abc"]
arr.last.upcase!
=> "ABC"
arr
=>["ABC","ABC","ABC"]     # which is **not** what I excepted

On the other hand:

arr = Array.new(3){"abc"}
=> ["abc","abc","abc"]
arr.last.upcase!
=>"ABC"
arr
=> ["abc","abc","ABC"]     # which is what I excepted
5
  • From the documentation: "Note that the second argument populates the array with references to the same object. Therefore, it is only recommended in cases when you need to instantiate arrays with natively immutable objects such as Symbols, numbers, true or false." Commented Aug 20, 2014 at 7:54
  • I'd add, just in case you're a Java programmer, that Ruby strings are mutable objects, not like Java ones. Commented Aug 20, 2014 at 9:25
  • 1
    I know this is a duplicate (has been asked numerous times already), but SO search is failing me. Anybody have a link at hand? Commented Aug 20, 2014 at 11:38
  • @JörgWMittag, I've had success using Google, with "site:stackoverflow.com" in the search string, when SO's search engine doesn't turn up what I'm looking for. Commented Sep 6, 2014 at 5:31
  • Ruby Array Commented Jul 4, 2019 at 6:38

1 Answer 1

8

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.