How do I use define_method to create class methods?
Asked Answered
C

6

118

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...

Calceolaria answered 15/4, 2009 at 17:3 Comment(0)
I
211

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
Intrigue answered 8/11, 2010 at 17:26 Comment(2)
also singleton_class.define_methodShrieval
@Shrieval Just to clarify, would you just go singleton_class.define_method :loudly do |message| etc.?Indirection
J
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
Jacaranda answered 15/4, 2009 at 17:21 Comment(4)
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.Calceolaria
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.Calceolaria
@Vincent Robert any link which would explain the magic of metaclass method?Interface
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.Jacaranda
A
9

This is the simplest way in Ruby 1.8+:

class A
  class << self
    def method_name
      ...
    end
  end
end
Adsorb answered 8/10, 2012 at 13:39 Comment(2)
I really like this one. Small, neat, reads well and it's portable. Of course, you could ask what I'm doing using ruby 1.8 in 2013...Kerbela
this doesn't answer the question: it's not metaprogramming and it's not using define_methodCartogram
T
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
Tunicate answered 19/11, 2015 at 6:47 Comment(0)
C
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).

Calceolaria answered 15/4, 2009 at 17:6 Comment(1)
Better (?) alternative may be to put things in a module and then have your create_class_method extend the module onto the class??? See: blog.jayfields.com/2008/07/ruby-underuse-of-modules.htmlCalceolaria
H
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
Howund answered 13/12, 2016 at 21:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.