Which method is called [...] in Ruby?

This code calls Array::[] with 1 , 2 and 3 as arguments:

 Array[1, 2, 3] #=> [1, 2, 3] 

But this is not like Array::[] :

 [1, 2, 3] #=> [1, 2, 3] 

So, which method is called [...] in Ruby?

Motivation: I'm trying to drown out a method in a test.

+4
source share
1 answer

This is the literal syntax for an array. This is not a message. Ruby, like most other languages, does not allow literals to be overloaded.

If you need literal overload, you should use a language that supports it, such as Ioke.

+5
source

Source: https://habr.com/ru/post/1436189/


All Articles