I read this article about enumerable-enumerator. map
is method of the Enumerable
:
module Enumerable
def map(*several_variants) ## `*several_variants` [https://stackoverflow.com/questions/28527931/definition-of-ruby-inbuilt-methods][2]
#This is a stub, used for indexing
end
end
class Enumerator # Enumerator class has included Enumerable module Ok fine !!!
include Enumerable # many thing are there ...
end
And in the Array
class:
class Array # Array class has included Enumerable module it means all the instances methods of Enumerable module will expose as the instance methods in Array class !!! Okk
include Enumerable # many thing are there ...
end
Now when I call the map
method on an array, I get an Enumerator
:
[1,2,3,4].map => #<Enumerator: [1, 2, 3, 4]:map>
1: What is Enumerator
in the #<Enumerator: [1, 2, 3, 4]:map>
line of output above?
module Test
def initialize(str)
@str = str
end
def methods_1(str)
p "Hello!!#{str}"
end
end
class MyTest
include Test
include Enumerable
end
my_test = MyTest.new('Ruby')
p "Which class ? : #{my_test}"
my_test.methods_1('Rails')
p my_test.map
Output
"Which class ? : #<MyTest:0x000000019e6e38>"
"Hello!!Rails"
#<Enumerator: #<MyTest:0x000000019e6e38 @str="Ruby">:map>
2: This should be object of MyTest class only if i'm not wrong.
3: In this line #<Enumerator: #<MyTest:0x000000019e6e38 @str="Ruby">:map>
, what does Enumerator
do here?
Thanks for all to making this concept clear, in short span of time. I would like to share some useful link to grasp the concept at great level.
Map, Select, and Other Enumerable Methods