Maybe I miss something. But I am confused about method call within a
class. What is the format for that? I just copy some code segments from
the forum as follows:
module Enumerable
def each_with_index
i = 0
each do |elem|
yield elem, i
i += 1
end
end
end
class Array
def each_with_index
size.times do |i|
yield self[i], i
end
end
end
“each”'s receiver will be an instance of a class that includes the
module Enumerable and “size”'s receiver will be an instance of the class
Array. Example:
array = Array.new
p array.size # => 0
class MyEnumerable
include Enumerable
end
foo = MyEnumerable.new
foo.each_with_index # doesn’t raise, the method is defined
“each”'s receiver will be an instance of a class that includes the
module Enumerable and “size”'s receiver will be an instance of the class
Array. Example:
array = Array.new
p array.size # => 0
class MyEnumerable
include Enumerable
end
foo = MyEnumerable.new
foo.each_with_index # doesn’t raise, the method is defined
Hi Andrea,
Thank you very much. So the receiver can be omitted when the method is
called within a class, right?