Skip to content

Instantly share code, notes, and snippets.

Last active Aug 29, 2015
What would you like to do?
Playing with Enumerable#to_a

Enumerable#to_a says, that it can take arguments also. Lets try

#ArgumentError: wrong number of arguments (2 for 0)

Oops! Why then error ? Because Enumerable#to_a called actually Range#each which don't accept any arguments. Now look the below code :-

class Foo
 include Enumerable
 def each(a, b, c)
   yield c
   yield b
   yield a
end, 2, 3)
# => [3, 2, 1]

Now, we can see that Enumerable#to_a takes argument, if the receiver's class implements the #each method to support it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment