116

This is useful if you are trying to create class methods metaprogramatically:

def self.create_methods(method_name)
    # To create instance methods:
    define_method method_name do
      ...
    end

    # To create class methods that refer to the args on create_methods:
    ???
end

My answer to follow...

Jon Seigel
  • 12,251
  • 8
  • 58
  • 92
Chinasaur
  • 2,108
  • 2
  • 16
  • 17

6 Answers6

208

I think in Ruby 1.9 you can do this:

class A
  define_singleton_method :loudly do |message|
    puts message.upcase
  end
end

A.loudly "my message"

# >> MY MESSAGE
fguillen
  • 36,125
  • 23
  • 149
  • 210
25

I prefer using send to call define_method, and I also like to create a metaclass method to access the metaclass:

class Object
  def metaclass
    class << self
      self
    end
  end
end

class MyClass
  # Defines MyClass.my_method
  self.metaclass.send(:define_method, :my_method) do
    ...
  end
end
Vincent Robert
  • 35,564
  • 14
  • 82
  • 119
  • 2
    Thanks! Definitely there are ways to make this nicer for yourself. But if you are working on an open source plugin, for example, I think it's nicer not to clog up the namespace with `metaclass`, so it's nice to know the easy, standalone shorthand. – Chinasaur Apr 16 '09 at 03:21
  • I decided to go with my original answer. My understanding is that using send() to access private methods if going away in Ruby 1.9, so that didn't seem like a good thing to use. Plus if you are defining more than one method, instance_evaling a block is cleaner. – Chinasaur May 06 '09 at 23:56
  • @Vincent Robert any link which would explain the magic of metaclass method? – Amol Pujari Aug 24 '12 at 03:54
  • class << self; self; end; simply reopens the class of self (class << self) and then returns that class (self) so actually returning the metaclass of self. – Vincent Robert Aug 24 '12 at 13:34
9

This is the simplest way in Ruby 1.8+:

class A
  class << self
    def method_name
      ...
    end
  end
end
Joseph Ravenwolfe
  • 6,480
  • 6
  • 31
  • 31
9

To be used in Rails if you want to define class methods dynamically from concern:

module Concerns::Testable
  extend ActiveSupport::Concern

  included do 
    singleton_class.instance_eval do
      define_method(:test) do
        puts 'test'
      end
    end
  end
end
Artur INTECH
  • 6,024
  • 2
  • 37
  • 34
7

Derived from: Jay and Why, who also provide ways to make this prettier.

self.create_class_method(method_name)
  (class << self; self; end).instance_eval do
    define_method method_name do
      ...
    end
  end
end

Update: from VR's contribution below; a more concise method (as long as you're only defining one method this way) that is still standalone:

self.create_class_method(method_name)
  (class << self; self; end).send(:define_method, method_name) do
    ...
  end
end

but note that using send() to access private methods like define_method() is not necessarily a good idea (my understanding is that it is going away in Ruby 1.9).

Chinasaur
  • 2,108
  • 2
  • 16
  • 17
  • Better (?) alternative may be to put things in a module and then have your create_class_method extend the module onto the class??? See: http://blog.jayfields.com/2008/07/ruby-underuse-of-modules.html – Chinasaur Sep 24 '09 at 14:21
0

You could also do something like this without relying on define_method:

A.class_eval do
  def self.class_method_name(param)
    puts param
  end
end

A.class_method_name("hello") # outputs "hello" and returns nil
Bijan
  • 25,559
  • 8
  • 79
  • 71