# Is there a way to call include for an object instance?

**URL:** <https://www.ruby-forum.com/t/is-there-a-way-to-call-include-for-an-object-instance/143188>\
**Category:** Ruby\
**Created:** [July 16, 2008, 5:42am UTC](https://www.ruby-forum.com/t/is-there-a-way-to-call-include-for-an-object-instance/143188 "2008-07-16T05:42:54Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![chrisolsen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c6cbf5/32.png) [@chrisolsen](https://www.ruby-forum.com/u/chrisolsen)\
**Post date:** [July 16, 2008, 5:42am UTC](https://www.ruby-forum.com/t/is-there-a-way-to-call-include-for-an-object-instance/143188/1 "2008-07-16T05:42:54Z")

</div>

I need to be able to create an object and, based on the param that I  
pass in on the class create\_for\_sport method, create an object with the  
proper module included.

It can be seen at the bottom by the comments I made that the includes  
are being added on to the Stats class and the methods from the first  
include are still there after the second include. Is there a way to add  
an include to an object instance rather than a class?

Thanks for the help

########  
module SoftballStats  
def to\_sport  
puts “softball”  
end

def batting\_average  
0.563  
end

def before\_save  
puts “in the softball before save”  
end

end

module HockeyStats  
def to\_sport  
puts “hockey”  
end

def scoring\_percentage  
0.34  
end

def before\_save  
puts “in the hockey before save”  
end

end

class Stats

def self.create\_by\_sport(sport)  
s = Stats.new  
s.sport = sport  
return s  
end

def sport=(sport)  
Stats.instance\_eval do # have tried to use self.instance\_eval  
eval(“include #{sport.capitalize}Stats”)  
end  
end

end

s = Stats.create\_by\_sport(“softball”)  
puts s.batting\_average # 0.563

h = Stats.create\_by\_sport(“hockey”)  
puts h.scoring\_percentage # 0.34  
puts h.batting\_average # 0.563 -\> still has this method

s.before\_save # “in the hockey save” -\> softball before\_save is now  
overridden  
h.before\_save # “in the hockey save”

---

<div class="post-metadata">

**Author:** ![chrisolsen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c6cbf5/32.png) [@chrisolsen](https://www.ruby-forum.com/u/chrisolsen)\
**Post date:** [July 16, 2008, 5:54am UTC](https://www.ruby-forum.com/t/is-there-a-way-to-call-include-for-an-object-instance/143188/2 "2008-07-16T05:54:40Z")

</div>

On Jul 15, 11:38 pm, Chris O. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> def before\_save  
> def scoring\_percentage
> 
> end  
> Couple of ways. One is to use the singleton.

def sport=(sport)  
(class \<\< self; self; end).class\_eval %{  
include #{sport.capitalize}Stats  
}  
end

But I think you might want to reconsider and use delegation instead.

T.

---

<div class="post-metadata">

**Author:** ![chrisolsen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c6cbf5/32.png) [@chrisolsen](https://www.ruby-forum.com/u/chrisolsen)\
**Post date:** [July 16, 2008, 6:12am UTC](https://www.ruby-forum.com/t/is-there-a-way-to-call-include-for-an-object-instance/143188/3 "2008-07-16T06:12:19Z")

</div>

Trans wrote:

> On Jul 15, 11:38ï¿½pm, Chris O. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > ï¿½ def before\_save  
> > ï¿½ def scoring\_percentage
> > 
> > ï¿½ end  
> > Couple of ways. One is to use the singleton.
> 
> def sport=(sport)  
> (class \<\< self; self; end).class\_eval %{  
> include #{sport.capitalize}Stats  
> }  
> end
> 
> But I think you might want to reconsider and use delegation instead.
> 
> T.

Thanks so much. To be honest I have no idea what the (class \<\< self;  
self; end), but it will make for good reading :). I will look into the  
delegation as well.

Thanks again.

---

<div class="post-metadata">

**Author:** ![chrisolsen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c6cbf5/32.png) [@chrisolsen](https://www.ruby-forum.com/u/chrisolsen)\
**Post date:** [July 16, 2008, 6:16am UTC](https://www.ruby-forum.com/t/is-there-a-way-to-call-include-for-an-object-instance/143188/4 "2008-07-16T06:16:19Z")

</div>

Hi Chris,

> I need to be able to create an object and, based on the param that I  
> pass in on the class create\_for\_sport method, create an object with the  
> proper module included.

You can call extend on an instance to mix in a modules instance  
methods to the instance (and leaving the class itself alone). In your  
code, we also need to convert your string to a reference to the module  
as well, via a call to const\_get. And this would be your new sport=  
method:

def sport=(sport)  
extend(self.class.const\_get(sport.capitalize + “Stats”))  
end

I believe that will work for you.

Eric

====

[LearnRuby.com](http://LearnRuby.com) offers Rails & Ruby HANDS-ON public & ON-SITE workshops.  
Please visit [http://LearnRuby.com](http://LearnRuby.com) for all the details.

---

<div class="post-metadata">

**Author:** ![chrisolsen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c6cbf5/32.png) [@chrisolsen](https://www.ruby-forum.com/u/chrisolsen)\
**Post date:** [July 16, 2008, 10:10am UTC](https://www.ruby-forum.com/t/is-there-a-way-to-call-include-for-an-object-instance/143188/5 "2008-07-16T10:10:56Z")

</div>

On Jul 16, 12:11 pm, “Eric I.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> method:
> 
> [LearnRuby.com](http://LearnRuby.com) offers Rails & Ruby HANDS-ON public & ON-SITE workshops.  
> Please visithttp://LearnRuby.comfor all the details.

you should put module(s) into class ,to ensure class Stats has the  
permission of module,so try following code

class Stats  
module HockeyStats  
def to\_sport  
puts “hockey”  
end

```
def scoring_percentage
  0.34
end

def before_save
  puts "in the hockey before save"
end

```

end

module SoftballStats  
def to\_sport  
puts “softball”  
end

```
  def batting_average
    0.563
  end

  def before_save
    puts "in the softball before save"
  end

```

end

def self.create\_by\_sport(sport)  
s = Stats.new  
s.sport = sport  
return s  
end

def sport=(sport)  
eval “extend #{sport.to\_s.capitalize}Stats”  
end

end

---

<div class="post-metadata">

**Author:** ![chrisolsen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c6cbf5/32.png) [@chrisolsen](https://www.ruby-forum.com/u/chrisolsen)\
**Post date:** [July 16, 2008, 5:04pm UTC](https://www.ruby-forum.com/t/is-there-a-way-to-call-include-for-an-object-instance/143188/6 "2008-07-16T17:04:54Z")

</div>

Eric I. wrote:

> def sport=(sport)  
> extend(self.class.const\_get(sport.capitalize + “Stats”))  
> end
> 
> I believe that will work for you.

That also works well.
