The Wayback Machine - https://web.archive.org/web/20070922232049/http://www.codeproject.com:80/useritems/Ruby_Dynamic_Methods.asp
4,526,168 members and growing!   5,622 now online.
Email Password Image Password problem?
All Topics, .NET, C# >> Cross Platform >> Unedited Reader Contributions  (Beginner)

Ruby - Add class methods at runtime.
By D Leverett.

Short article showing how to add methods to a class instance at runtime.
Generic
Windows
Win32, VS
Dev
Posted: 1 Sep 2006
Updated: 15 Sep 2006
Views: 9,646
Note: This is an unedited reader contribution  
Announcements


Image
Search    
Advanced Search
Sitemap
Image PrintImageBroken Article?ImageBookmarkImage DiscussImage Send to a friend
1 vote for this article.
Image Image Image Image Image
Popularity: 0. Rating: 4 out of 5.

Sample Image - Ruby_Dynamic_Methods.jpg

Introduction

This is a very brief article about one great feature of the Ruby language - dynamically adding methods to classes at runtime.

Basically I have been learning the Ruby language and thought I would share few snippets with people in small articles, so if you are not familiar with Ruby take a quick look and see what you think - you never know it might spark your interest!

Adding methods to class instances

One great feature of Ruby is that it allows you to add methods to individual class instances on the fly. The code is pretty self explanatory so I won't go into any details.

Update - 15 Sep 2006

- In response to some comments regarding how you use reflection etc in ruby I have added a new block of code showing how to reflect against a class, call methods dynamically and a simple benchmark showing the timings for each method.

##   ----  Add methods to a class instance at runtime. ---- ##

# Create a very simple class.
<CODE __designer:dtid="281474976710672">class TestAddMethod
    def method1
        puts "Method1 Called."
    end
end


# Create a new instance of this class.
t = TestAddMethod.new


# Add a method to this instance.
def t.method2
    puts "Method2 Called."
end

# Call both methods.
t.method1
t.method2

# Output: 
#        Method1 Called.
#        Method2 Called.

# Override existing method with new functionality.
def t.method1
    puts "I replaced the normal method with this."
end

# Call the modified method.
t.method1

# Output: 
#        I replaced the normal method with this.

# Add to existing methods.
def t.method1
    puts "Again I replaced the normal method with this, now I am going to call the method on the original class: "
    # Calls the original method defined at the top (on this instances SuperClass).
    super 
end

# Call the modified method.
t.method1

# Output: 
#        Again I replaced the normal method with this, now I am going to call the method on the original class: 
#        Method1 Called.

------- Section below added in response to some questions/comments from Baj22:

require "benchmark"  
include Benchmark

# Define a simple class.
class TestClass
    
    def method1
        puts "Called!"
    end
  
    def method2
        puts "Called!"
      end
      
    def method3
        puts "Called!"
    end
      
    def method4
        
    end
      
  end 
  
  # Define a simple class.
class TestClass2 < TestClass
    def method4
        puts "Called!"
    end
end 

# List the methods supported.
puts "TestClass supports #{TestClass.methods.length} method(s)" # Result: TestClass supports 76 method(s)
TestClass.methods.each do |method|
  puts "\t #{method}"
end
# Result:
#   to_a
#	 respond_to?
#	 display
#	 etc, etc


# List the methods supported.
t = TestClass.new()
puts "T Instance supports #{t.methods.length} method(s)" #' Result: T Instance supports 46 method(s)
t.methods.each do |method|
  puts "\t #{method}"
end
# Result:
#   to_a
#	 respond_to?
#	 display
#	 etc, etc

# Check if a class supports a method by name.
puts "r responds to 'Method1' = #{t.respond_to?("method1")}" # Result: true.
puts "r responds to 'UNKNOWN' = #{t.respond_to?("UNKNOWN")}" # Result: false.


# Walk the inheritance tree.
cls = TestClass2
begin
  print cls
  cls = cls.superclass
  print " < " if cls
end while cls           # Result: TestClass2 < TestClass < Object


# Call a method dynimically.
t.send(:method1)  # Result: Called!

method1 = t.method(:method1)
method1.call   # Result: Called!
method4 = t.method(:method4)

# Test dynamic calls.
n = 100000

bm(12) {|x|
  x.report("Method.Call") { n.times { method4.call } }
  x.report("Instance.send") { n.times { t.send(:method4) } }
  x.report("Instance.Method") { n.times { t.method4} }
}
#Result 
#                         user     system      total        real
#Method.Call         0.040000   0.000000   0.040000 (  0.060000)
#Instance.send      0.080000   0.000000   0.080000 (  0.090000)
#Instance.Method  0.050000   0.000000   0.050000 (  0.051000)

More Info

There are other ways to add methods to classes - one is to use a mix-in - where you essentially include all of the methods from a module in a class definition. This allows you to mimic some of the multiple inheritance functionality seen in C++.

Anyway - thanks for your time. Any comments/questions are welcome. As I play with Ruby more I will try to post more articles on things that I find interesting.

History

Version 1.1

About D Leverett


D Leverett is a software developer who has worked with a variaty of technologies over the years.

I enjoy learning new languages/skills and I am currently working my way through Ruby/Rails.

I am based in the UK and currently work for a recruitment CRM vendor.

MCSD(.NET), MCAD(.NET), MCTS(SQL 2005), MCSD(VB6/COM).

Click here to view D Leverett's online profile.


Other popular Cross Platform articles:

[Top] Sign in to vote for this article:     PoorExcellent  
Note: You must Sign in to post to this message board.
Hint: For a faster board use IE 4+ or Mozilla, choose 'Message View' from the View dropdown and hit 'Set Options'.
Image FAQ Message score threshold   Image Search comments  
 View   Per page  
 Msgs 1 to 3 of 3 (Total: 3) (Refresh)[First] [Prev] [Next] [    ]
Subject 
Image
Author 
Image
Image
Date 
Image
Image
Image  Decorator
Image LucioLucio 9:51 19 Sep '06 
Image
Image
Image  Dynamic Methods
Image Baj22 15:05 13 Sep '06 
Image
ImageImage  Re: Dynamic Methods
Image D Leverett 5:52 15 Sep '06 
Image
Last Visit: 19:20 Saturday 22nd September, 2007[First] [Prev] [Next] [    ]

Image General comment    Image News / Info    Image Question    Image Answer    Image Joke / Game    Image Admin message


Updated: 15 Sep 2006 Article content copyright D Leverett, 2006
everything else Copyright � CodeProject, 1999-2007.
Web08 | Advertise on The Code Project | Privacy

Image
The Ultimate Toolbox � ASP Alliance � Developer Fusion � Developersdex � DevGuru � Programmers Heaven � Planet Source Code � Tek-Tips Forums �