# Add a method to a class at runtime?

**URL:** <https://www.ruby-forum.com/t/add-a-method-to-a-class-at-runtime/113409>\
**Category:** Ruby\
**Created:** [August 30, 2007, 8:55pm UTC](https://www.ruby-forum.com/t/add-a-method-to-a-class-at-runtime/113409 "2007-08-30T20:55:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bwv549](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ac8455/32.png) [@bwv549](https://www.ruby-forum.com/u/bwv549)\
**Post date:** [August 30, 2007, 8:55pm UTC](https://www.ruby-forum.com/t/add-a-method-to-a-class-at-runtime/113409/1 "2007-08-30T20:55:42Z")

</div>

module SomeModule  
def happy?  
puts ‘yes’  
end  
end

# I can do this:

some\_object.extend(SomeModule)  
some\_object.happy? # -\> ‘yes’

# But I really need to give the class the method (not just instances)

# because I have a collection of hundreds of thousands of objects

# and need them all to have the method quickly.

# The problem is that I don’t know the class that needs to include the

method

# until runtime!

# This won’t work:

some\_object.class.include(SomeModule)

# -\> NoMethodError: private method `include’ called for

# What is the workaround to add a method to a class (known only at

runtime)?

Thanks

---

<div class="post-metadata">

**Author:** ![bwv549](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ac8455/32.png) [@bwv549](https://www.ruby-forum.com/u/bwv549)\
**Post date:** [August 30, 2007, 9:03pm UTC](https://www.ruby-forum.com/t/add-a-method-to-a-class-at-runtime/113409/2 "2007-08-30T21:03:43Z")

</div>

Note to self,  
class\_eval or module\_eval should work

# from ri class\_eval:

class Thing  
end  
a = %q{def hello() “Hello there!” end}  
Thing.module\_eval(a)  
puts Thing.new.hello()  
Thing.module\_eval(“invalid code”, “dummy”, 123)

---

<div class="post-metadata">

**Author:** ![bwv549](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ac8455/32.png) [@bwv549](https://www.ruby-forum.com/u/bwv549)\
**Post date:** [August 30, 2007, 10:30pm UTC](https://www.ruby-forum.com/t/add-a-method-to-a-class-at-runtime/113409/3 "2007-08-30T22:30:51Z")

</div>

On Aug 30, 12:58 pm, bwv549 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> # from ri class\_eval:
> 
> class Thing  
> end  
> a = %q{def hello() “Hello there!” end}  
> Thing.module\_eval(a)  
> puts Thing.new.hello()  
> Thing.module\_eval(“invalid code”, “dummy”, 123)

And no need to use strings:  
module Joi  
def happy?; true; end  
end

class Foo; end

f1 = Foo.new  
f2 = Foo.new

f2.class.class\_eval{ include Joi }  
p f1.happy?  
#=\> true

---

<div class="post-metadata">

**Author:** ![bwv549](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ac8455/32.png) [@bwv549](https://www.ruby-forum.com/u/bwv549)\
**Post date:** [August 30, 2007, 10:33pm UTC](https://www.ruby-forum.com/t/add-a-method-to-a-class-at-runtime/113409/4 "2007-08-30T22:33:11Z")

</div>

bwv549 wrote:

> module SomeModule  
> def happy?  
> puts ‘yes’  
> end  
> end
> 
> # I can do this:
> 
> some\_object.extend(SomeModule)  
> some\_object.happy? # -\> ‘yes’
> 
> # But I really need to give the class the method (not just instances)
> 
> # because I have a collection of hundreds of thousands of objects
> 
> # and need them all to have the method quickly.
> 
> # The problem is that I don’t know the class that needs to include the
> 
> method
> 
> # until runtime!
> 
> # This won’t work:
> 
> some\_object.class.include(SomeModule)
> 
> # -\> NoMethodError: private method `include’ called for
> 
> # What is the workaround to add a method to a class (known only at
> 
> runtime)?
> 
> Thanks

You have a misunderstanding here. _All_ methods are added to classes at  
runtime, since that’s the only “time” ruby has (there is no compile  
time). Of course, C-extensions are excluded from this.  
You can always reopen a class:  
class Array  
def foo; “foo!”; end  
end  
[].foo # =\> “foo!”

Regards  
Stefan

---

<div class="post-metadata">

**Author:** ![bwv549](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ac8455/32.png) [@bwv549](https://www.ruby-forum.com/u/bwv549)\
**Post date:** [August 30, 2007, 10:35pm UTC](https://www.ruby-forum.com/t/add-a-method-to-a-class-at-runtime/113409/5 "2007-08-30T22:35:43Z")

</div>

Stefan R. wrote:

> bwv549 wrote:
> 
> > module SomeModule  
> > def happy?  
> > puts ‘yes’  
> > end  
> > end
> > 
> > # I can do this:
> > 
> > some\_object.extend(SomeModule)  
> > some\_object.happy? # -\> ‘yes’
> > 
> > # But I really need to give the class the method (not just instances)
> > 
> > # because I have a collection of hundreds of thousands of objects
> > 
> > # and need them all to have the method quickly.
> > 
> > # The problem is that I don’t know the class that needs to include the
> > 
> > method
> > 
> > # until runtime!
> > 
> > # This won’t work:
> > 
> > some\_object.class.include(SomeModule)
> > 
> > # -\> NoMethodError: private method `include’ called for
> > 
> > # What is the workaround to add a method to a class (known only at
> > 
> > runtime)?
> > 
> > Thanks
> 
> You have a misunderstanding here. _All_ methods are added to classes at  
> runtime, since that’s the only “time” ruby has (there is no compile  
> time). Of course, C-extensions are excluded from this.  
> You can always reopen a class:  
> class Array  
> def foo; “foo!”; end  
> end  
> [].foo # =\> “foo!”
> 
> Regards  
> Stefan

That one went off too early, The missing paragraph:  
As for your problem: you can use include, e.g. via  
YourClass.send(:include, ModuleName), if you have the classname as  
Symbol or String, see Module#const\_get.

Regards  
Stefan
