# How to add a "static method" dynamic?

**URL:** <https://www.ruby-forum.com/t/how-to-add-a-static-method-dynamic/88767>\
**Category:** Ruby\
**Created:** [March 14, 2007, 7:17am UTC](https://www.ruby-forum.com/t/how-to-add-a-static-method-dynamic/88767 "2007-03-14T07:17:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![yananay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/5daacb/32.png) [@yananay](https://www.ruby-forum.com/u/yananay)\
**Post date:** [March 14, 2007, 7:17am UTC](https://www.ruby-forum.com/t/how-to-add-a-static-method-dynamic/88767/1 "2007-03-14T07:17:03Z")

</div>

now , i know that i can use  
“define\_method” add a method dynamic,  
but i want to know how to add a “static method” dynamic ?

thanks!

---

<div class="post-metadata">

**Author:** ![yananay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/5daacb/32.png) [@yananay](https://www.ruby-forum.com/u/yananay)\
**Post date:** [March 14, 2007, 12:31pm UTC](https://www.ruby-forum.com/t/how-to-add-a-static-method-dynamic/88767/2 "2007-03-14T12:31:24Z")

</div>

> now , i know that i can use “define\_method” add a method  
> dynamic, but i want to know how to add a “static method”  
> dynamic ?

You can do this by using define\_method in the context of the  
singleton class of a class:

class \<\< aClass  
define\_method …  
end

In Ruby, we prefer to call this a “class method”. We don’t use  
the term “static method”.

gegroet,  
Erik V. - [http://www.erikveen.dds.nl/](http://www.erikveen.dds.nl/)

* * *

##############################################################

class Foo  
class \<\< self  
define\_method :one do  
1  
end  
end

class \<\< self  
self  
end.module\_eval do  
define\_method :two do  
2  
end  
end  
end

##############################################################

class Bar  
end

class \<\< Bar  
define\_method :one do  
1  
end  
end

class \<\< Bar  
self  
end.module\_eval do  
define\_method :two do  
2  
end  
end

##############################################################

p Foo.one  
p Foo.two

p Bar.one  
p Bar.two

##############################################################

---

<div class="post-metadata">

**Author:** ![yananay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/5daacb/32.png) [@yananay](https://www.ruby-forum.com/u/yananay)\
**Post date:** [March 14, 2007, 12:44pm UTC](https://www.ruby-forum.com/t/how-to-add-a-static-method-dynamic/88767/3 "2007-03-14T12:44:25Z")

</div>

On 3/14/07, Yan R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> now , i know that i can use  
> “define\_method” add a method dynamic,  
> but i want to know how to add a “static method” dynamic ?
> 
> thanks!
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

There are no static methods, there are only instance methods.  
But as about everything in Ruby is an Object, so is the class of your  
object and so is the singleton class of your object, maybe the  
following tricks are what you are looking for:  
707/475 \> cat class.rb && ruby class.rb

# vim: sts=2 sw=2 tw=0 expandtab nu:

class A  
define\_method :one do |p| “one(#{p})” end  
class \<\< self  
define\_method :two do |p| “two(#{p})” end  
end  
eval ‘def self.three p ; “three(#{p})” end’  
end

puts A.new.one(42)  
puts A.two(42)  
puts A.three(42)  
one(42)  
two(42)  
three(42)

Sorry for the double post Erik but I was not about to discard all that  
hard work 😉  
Please note the ugliness of the eval for :three but it gets the job  
done.  
Personally I almost always use the singleton class method definition  
even when declaring statically, but I believe that it is not _exactly_  
the same thing.

Cheers  
Robert
