# Creating and adding new methods in runtime

**URL:** <https://www.ruby-forum.com/t/creating-and-adding-new-methods-in-runtime/103930>\
**Category:** Ruby\
**Created:** [May 11, 2007, 7:14pm UTC](https://www.ruby-forum.com/t/creating-and-adding-new-methods-in-runtime/103930 "2007-05-11T19:14:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![konryd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/bcef8e/32.png) [@konryd](https://www.ruby-forum.com/u/konryd)\
**Post date:** [May 11, 2007, 7:14pm UTC](https://www.ruby-forum.com/t/creating-and-adding-new-methods-in-runtime/103930/1 "2007-05-11T19:14:01Z")

</div>

Hello, I need to add in runtime several methods to my class. How can I  
accomplish this in ruby?  
More precisely: I need my class to overload arithmetic operators with  
fixnums in both ways (4+MyClass, MyClass+4). All the necessary info is  
stored in a hash, so I created a method to do all the stuff. It looks  
like do\_operation(operator, other)  
But still I have to add every single operator overloader:  
class Fixnum

def doo\_operation(operator, other)  
#…nevermind…  
end

def +(other)  
do\_operation(:+, other)  
end  
def -(other)  
do\_operation(:-, other)  
end  
end

can I automate it somehow?

---

<div class="post-metadata">

**Author:** ![konryd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/bcef8e/32.png) [@konryd](https://www.ruby-forum.com/u/konryd)\
**Post date:** [May 11, 2007, 7:15pm UTC](https://www.ruby-forum.com/t/creating-and-adding-new-methods-in-runtime/103930/2 "2007-05-11T19:15:03Z")

</div>

On Fri, 11 May 2007 08:06:54 -0700, konryd wrote:

> end
> 
> def +(other)  
> do\_operation(:+, other)  
> end  
> def -(other)  
> do\_operation(:-, other)  
> end  
> end
> 
> can I automate it somehow?

Just overrode MyClass#coerce, and the numeric operators on other classes  
will call coerce if they can’t identify the class you’re trying to  
perform arithmetic. Make sure there are +,-, etc… operators on  
MyClass,  
and make sure that they  
(a) handle built-in numeric types  
(b) handle MyClass+MyClass  
© call coerce if they don’t recognize the class on the right-hand side

There’s more discussion of #coerce in PickAxe.

–Ken

---

<div class="post-metadata">

**Author:** ![konryd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/bcef8e/32.png) [@konryd](https://www.ruby-forum.com/u/konryd)\
**Post date:** [September 25, 2007, 11:04pm UTC](https://www.ruby-forum.com/t/creating-and-adding-new-methods-in-runtime/103930/3 "2007-09-25T23:04:09Z")

</div>

konryd wrote:

> #…nevermind…  
> can I automate it somehow?

the simplest case someone might come up with is:

module Operators

```
 def operation operator, other
     puts "operator #{operator} with argument #{other} called"
 end

 OPERATORS = [:+,:-,:/,:*]

 def self.included mod
     mod.instance_eval do
         OPERATORS.each do |op|
             begin
                 instance_method(op)
                 alias_method(:"_#{op}", op)
             rescue
                 # swallow NameError here
             end

             define_method op do |other|
                 # here you might want to check for 'other' class to

```

preserve Numeric/Enumerable behaviour  
operation op, other  
end  
end  
end  
end

end

class Fixnum  
include Operators  
end

1 + 2  
1.send :"\_+" ,2 # original

class A  
include Operators  
end

A.new + A.new

lopex
