# Method call within a class

**URL:** <https://www.ruby-forum.com/t/method-call-within-a-class/186696>\
**Category:** Ruby\
**Created:** [April 11, 2010, 7:48pm UTC](https://www.ruby-forum.com/t/method-call-within-a-class/186696 "2010-04-11T19:48:24Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [April 11, 2010, 7:48pm UTC](https://www.ruby-forum.com/t/method-call-within-a-class/186696/1 "2010-04-11T19:48:24Z")

</div>

Hi all,

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

So

1. which one is the reciever for “each”?
2. which one is the reciever for “size”?

Do they default to self?

Thanks,

Li

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [April 11, 2010, 8:29pm UTC](https://www.ruby-forum.com/t/method-call-within-a-class/186696/2 "2010-04-11T20:29:09Z")

</div>

Hei,

“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

HTH

–  
Andrea D.

> **[GitHub - bolthar/freightrain: Ruby desktop development made easy](https://github.com/bolthar/freightrain)**
>
> Ruby desktop development made easy. Contribute to bolthar/freightrain development by creating an account on GitHub.

> **[using (IMHO)](https://usingimho.wordpress.com)**
>
> Andrea Dallera 's blog

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [April 11, 2010, 9:15pm UTC](https://www.ruby-forum.com/t/method-call-within-a-class/186696/3 "2010-04-11T21:15:38Z")

</div>

Andrea D. wrote:

> Hei,
> 
> “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?

Li

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [April 11, 2010, 11:36pm UTC](https://www.ruby-forum.com/t/method-call-within-a-class/186696/4 "2010-04-11T23:36:28Z")

</div>

Hei Li,

yes, the default deceiver is ‘self’ so, if omitted, the receiver for  
any message will be the istance itself.

–  
Andrea D.

> **[GitHub - bolthar/freightrain: Ruby desktop development made easy](https://github.com/bolthar/freightrain)**
>
> Ruby desktop development made easy. Contribute to bolthar/freightrain development by creating an account on GitHub.

> **[using (IMHO)](https://usingimho.wordpress.com/)**
>
> Andrea Dallera 's blog

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [April 12, 2010, 1:25am UTC](https://www.ruby-forum.com/t/method-call-within-a-class/186696/5 "2010-04-12T01:25:54Z")

</div>

On Sun, Apr 11, 2010 at 2:15 PM, Li Chen [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi Andrea,
> 
> Thank you very much. So the receiver can be omitted when the method is  
> called within a class, right?
> 
> ## Li
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

Except on assignments, because it looks like you are creating a local  
variable.  
Example:

class Foo  
attr\_accessor :bar  
def bad\_assign  
bar = ‘baz’  
defined? bar  
end  
def good\_assign  
self.bar = ‘baz’  
defined? bar  
end  
end

foo = Foo.new

foo.bad\_assign # =\> “local-variable”  
foo.bar # =\> nil

foo.good\_assign # =\> “method”  
foo.bar # =\> “baz”

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [April 12, 2010, 2:34am UTC](https://www.ruby-forum.com/t/method-call-within-a-class/186696/6 "2010-04-12T02:34:45Z")

</div>

Josh C. wrote:

> On Sun, Apr 11, 2010 at 2:15 PM, Li Chen [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Hi Andrea,  
> > Except on assignments, because it looks like you are creating a local  
> > variable.  
> > Example:
> 
> class Foo  
> attr\_accessor :bar  
> def bad\_assign  
> bar = ‘baz’  
> defined? bar  
> end  
> def good\_assign  
> self.bar = ‘baz’  
> defined? bar  
> end  
> end
> 
> foo = Foo.new
> 
> foo.bad\_assign # =\> “local-variable”  
> foo.bar # =\> nil
> 
> foo.good\_assign # =\> “method”  
> foo.bar # =\> “baz”

Thank you so much. Where can I find more info about this usage?

Li
