# Surprising behavior when extending instances

**URL:** <https://www.ruby-forum.com/t/surprising-behavior-when-extending-instances/178974>\
**Category:** Ruby\
**Created:** [November 14, 2009, 3:50pm UTC](https://www.ruby-forum.com/t/surprising-behavior-when-extending-instances/178974 "2009-11-14T15:50:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jimc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ba8739/32.png) [@jimc](https://www.ruby-forum.com/u/jimc)\
**Post date:** [November 14, 2009, 3:50pm UTC](https://www.ruby-forum.com/t/surprising-behavior-when-extending-instances/178974/1 "2009-11-14T15:50:47Z")

</div>

This surprised me:

module English; def say; “yes”; end; end  
module French; def say; “oui”; end; end  
x = Object.new  
x.extend English  
x.say # “yes”  
x.extend French  
x.say # “oui”  
x.extend English  
x.say # “oui”

What wrong assumptions am I making that this surprises me? What is  
the more elegant “Ruby way” of alternating an object’s behavior at  
runtime?

Thanks,  
Jim

---

<div class="post-metadata">

**Author:** ![jimc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ba8739/32.png) [@jimc](https://www.ruby-forum.com/u/jimc)\
**Post date:** [November 14, 2009, 5:15pm UTC](https://www.ruby-forum.com/t/surprising-behavior-when-extending-instances/178974/2 "2009-11-14T17:15:03Z")

</div>

Hi,

In message “Re: Surprising behavior when extending instances”  
on Sat, 14 Nov 2009 23:50:05 +0900, Jim C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
writes:

|This surprised me:  
|  
| module English; def say; “yes”; end; end  
| module French; def say; “oui”; end; end  
| x = Object.new  
| x.extend English  
| x.say # “yes”  
| x.extend French  
| x.say # “oui”  
| x.extend English  
| x.say # “oui”  
|  
|What wrong assumptions am I making that this surprises me? What is  
|the more elegant “Ruby way” of alternating an object’s behavior at  
|runtime?

#extend (and #include) include a module only once.

```
          matz.

```

---

<div class="post-metadata">

**Author:** ![jimc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ba8739/32.png) [@jimc](https://www.ruby-forum.com/u/jimc)\
**Post date:** [November 14, 2009, 5:26pm UTC](https://www.ruby-forum.com/t/surprising-behavior-when-extending-instances/178974/3 "2009-11-14T17:26:10Z")

</div>

Jim C.:

> This surprised me:

> module English; def say; “yes”; end; end  
> module French; def say; “oui”; end; end  
> x = Object.new  
> x.extend English

This adds English to the front of the method look-up path.

> x.say # “yes”

At this point, the #say method is first looked-up in English.

> x.extend French

This adds French to the front of the method look-up path.

> x.say # “oui”

At this point, the #say method is first looked-up in French.

> x.extend English

Hey, English is already in the method look-up path, no need to add it!

> x.say # “oui”

Oui, French is still at the front of the method look-up pathâ€¦

> What wrong assumptions am I making that this surprises me?

That the second #extend call moved English  
to the front of the method look-up path.

> What is the more elegant “Ruby way” of  
> alternating an object’s behavior at runtime?

I donâ€™t have hand-on experience, and there probably is a simple,  
canonical way, but Iâ€™d start with looking at Object#instance\_eval.

â€” Shot

---

<div class="post-metadata">

**Author:** ![jimc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ba8739/32.png) [@jimc](https://www.ruby-forum.com/u/jimc)\
**Post date:** [November 14, 2009, 5:39pm UTC](https://www.ruby-forum.com/t/surprising-behavior-when-extending-instances/178974/4 "2009-11-14T17:39:38Z")

</div>

On Sat, Nov 14, 2009 at 9:50 AM, Jim C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> x.say # “oui”
> 
> What wrong assumptions am I making that this surprises me?

Matz has already explained that module inclusion is a one-time only  
deal.

Here’s a bit on why that is.

[http://talklikeaduck.denhaven2.com/2007/11/03/a-chat-with-matz-classs-variable-reversion-and-a-mystery-explained](http://talklikeaduck.denhaven2.com/2007/11/03/a-chat-with-matz-classs-variable-reversion-and-a-mystery-explained)

> What is  
> the more elegant “Ruby way” of alternating an object’s behavior at  
> runtime?

I’d use a form of delegation

module English; def self.agree; “yes”; end; end  
module French; def self.agree; “oui”; end; end

class Speaker

attr\_accessor :language

def initialize(language = English)  
self.language = language  
end

def say  
language.agree  
end

def speak(language)  
self.language = language  
end  
end

jack = Speaker.new  
jacques = Speaker.new(French)

jack.say # =\> “yes”  
jacques.say # =\> “oui”

jack.speak(French)  
jacques.speak(English)

jack.say # =\> “oui”  
jacques.say # =\> “yes”

jack.speak(English)  
jacques.speak(French)

jack.say # =\> “yes”  
jacques.say # =\> “oui”

–  
Rick DeNatale

Blog: [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)  
Twitter: [http://twitter.com/RickDeNatale](http://twitter.com/RickDeNatale)  
WWR: [NameBright - Coming Soon](http://www.workingwithrails.com/person/9021-rick-denatale)  
LinkedIn: [http://www.linkedin.com/in/rickdenatale](http://www.linkedin.com/in/rickdenatale)
