# Question about Mixins and calling a method

**URL:** <https://www.ruby-forum.com/t/question-about-mixins-and-calling-a-method/149112>\
**Category:** Ruby\
**Created:** [September 20, 2008, 7:18pm UTC](https://www.ruby-forum.com/t/question-about-mixins-and-calling-a-method/149112 "2008-09-20T19:18:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![caof2005](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/97f17d/32.png) [@caof2005](https://www.ruby-forum.com/u/caof2005)\
**Post date:** [September 20, 2008, 7:18pm UTC](https://www.ruby-forum.com/t/question-about-mixins-and-calling-a-method/149112/1 "2008-09-20T19:18:51Z")

</div>

Hi folks.  
I tried to reproduce a Mixin example that is include in the Pick Axe  
book, here’s the code:

module Observable  
def observers  
@observer\_list ||= []  
end

def add\_observer( obj )

# ----???

```
observers << obj

```

end

def print\_observers  
@observer\_list.each{ |i| puts i }  
end

end

def notify\_observers  
# ----???  
observers.each {|o| o.update }  
end

class TelescopeScheduler  
include Observable

def initialize  
@observer\_list = []  
end

def add\_viewer( viewer )  
@observer\_list \<\< viewer  
end  
end

visor1 = “Carlos”  
visor2 = “Ana”  
my\_telescope = TelescopeScheduler.new  
my\_telescope.add\_viewer(visor1)  
my\_telescope.add\_viewer(visor2)  
my\_telescope.print\_observers

* * *

It works great however while reviewing the code I noticed a couple of  
situations that I really don’t understand:

1. Is it possible to invoke a method with a “\<\<” notation ?

What I mean is that inside the “add\_observer” method I’m calling  
observers using \<\< instead of parenthesis:

def add\_observer( obj )  
observers \<\< obj  
end

On the other side while reviewing the “observers” method, I noticed that  
there is no parameter in its definition:  
def observers  
@observer\_list ||= []  
end

Is this valid ? ( I suppose it is because it works ), but how does this  
works ?

1. Same situation happens in the “notify\_observers” method, in it I can  
invoke “observers.each” although it is not defined anywhere. Why can I  
do that?

Any help with this questions would be very appreciated.  
Best Regards  
Carlos

---

<div class="post-metadata">

**Author:** ![caof2005](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/97f17d/32.png) [@caof2005](https://www.ruby-forum.com/u/caof2005)\
**Post date:** [September 20, 2008, 8:22pm UTC](https://www.ruby-forum.com/t/question-about-mixins-and-calling-a-method/149112/2 "2008-09-20T20:22:40Z")

</div>

Carlos O. wrote:

> 1. Is it possible to invoke a method with a “\<\<” notation ?

It is not ‘invoking a method with “\<\<” notation,’ it is simply calling  
the method named observers without any arguments, and then calling \<\< on  
the result of the method. This is equivalent to:  
(@observer\_list||=[])\<\<obj  
or even:  
(@observer\_list||=[]).\<\<(obj)

Now: the method observers initialises the instance variable  
@observer\_list. The operator ||= assigns the empty array to the variable  
if the variable is still nil at the moment of the call (so it will  
assign the empty array only on the first call), and then return the  
variable, no mater if the assignment was done or not. So the line  
observers\<\<obj does the following:  
@observer\_list=[] if not @observer\_list  
@observer\_list\<\<obj

> On the other side while reviewing the “observers” method, I noticed that  
> there is no parameter in its definition:  
> Parentheses can be skipped both in definition and in call of a methods,  
> if there are no parameters.

> 1. Same situation happens in the “notify\_observers” method, in it I can  
> invoke “observers.each” although it is not defined anywhere. Why can I  
> do that?

Again, this is a call to the method observers, and then a call to the  
method each of the result you got. The method observers never returns  
nil, because it either initialises or just returns the already  
initialised @observer\_list.

The instance variable @observer\_list itself is not defined anywhere  
though, because you don’t have to declare instance variables in any way.  
Simply if you try to read a variable for the first time, it returns nil  
if the variable has not been assigned before.

You can read more about classes and their features here:

> **[class Class](http://al2o3-cr.blogspot.com/2008/08/class-class.html)**
>
> Today, let's have a look at classes in Ruby. You can get basic information about these animals under the links on the right, here I'm going ...

TPR.

---

<div class="post-metadata">

**Author:** ![caof2005](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/97f17d/32.png) [@caof2005](https://www.ruby-forum.com/u/caof2005)\
**Post date:** [September 20, 2008, 8:32pm UTC](https://www.ruby-forum.com/t/question-about-mixins-and-calling-a-method/149112/3 "2008-09-20T20:32:59Z")

</div>

On Sat, Sep 20, 2008 at 1:10 PM, Carlos O. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> # ----???
> 
> # ----???
> 
> def add\_observer( obj )  
> observers \<\< obj  
> end

observers is a method defined in the Observable module. I’ll address \<\<  
after…

On the other side while reviewing the “observers” method, I noticed that

> there is no parameter in its definition:  
> def observers  
> @observer\_list ||= []  
> end

> Is this valid ? ( I suppose it is because it works ), but how does this  
> works ?

Sure it is valid there’s no rule that a method must take a parameter,  
lots  
don’t.

So what is happening in the add\_observer method is that the observers  
method is invoked, it uses a lazy initialization pattern to initialize  
and  
instance variable to an empty array if necessary, and returns that  
instance  
variable.

Note that this is interpreted the same as if it had been:

def add\_observer(obj)  
self.observers \<\< obj  
end

the self is implied.

Then the \<\< message is sent to the result of the observers method with  
obj  
as the parameter.

> 1. Same situation happens in the “notify\_observers” method, in it I can  
> invoke “observers.each” although it is not defined anywhere. Why can I  
> do that?

Same thing each is being sent to the object resulting from the observers  
method.

Now lets look at your usage of the module:

> class TelescopeScheduler  
> include Observable

> def initialize  
> @observer\_list = []  
> end

This is unnecessary, since @observer\_list is initialized by the module  
method when needed.

> def add\_viewer( viewer )  
> @observer\_list \<\< viewer  
> end

This isn’t really using the module. The whole TelescopeScheduler class,  
as  
written, can be just:

class TelescopeScheduler  
include Observable

```
alias_method :add_viewer, :add_observer

```

end

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)
