I am quite new to Ruby, so still learning. I was researching quite a bit about how to add methods dynamically, and I was successful to create instance methods, but not successful when creating class methods.
我是Ruby的新手,所以还在学习。我正在研究如何动态添加方法,我成功创建了实例方法,但在创建类方法时却没有成功。
This is how I generated instance methods:
这就是我生成实例方法的方法:
class B
def before_method
puts "before method"
end
def self.run(method)
send :define_method, method do
before_method
puts "method #{method}"
end
end
end
class A < B
run :m
run :n
end
Any idea about the best ways to create static methods?
有关创建静态方法的最佳方法的任何想法?
My final task is to look for the best way to create "before" and "after" tasks for class methods.
我的最后一项任务是寻找为类方法创建“之前”和“之后”任务的最佳方法。
3 个解决方案
#1
1
Here's something re-worked to use class methods:
这里有一些重新使用类方法的东西:
class B
def self.before_method
puts "before method"
end
def self.run(method)
define_singleton_method(method) do
before_method
puts "method #{method}"
end
end
end
Update: Using define_singleton_method
from Ruby 1.9 which properly assigns to the eigenclass.
更新:使用Ruby 1.9中的define_singleton_method正确分配给特征类。
#2
25
To create instance methods dynamically, try
要动态创建实例方法,请尝试
class Foo
LIST = %w(a b c)
LIST.each do |x|
define_method(x) do |arg|
return arg+5
end
end
end
Now any instance of Foo would have the method "a", "b", "c". Try
现在任何Foo实例都有方法“a”,“b”,“c”。尝试
Foo.new.a(10)
To define class methods dynamically, try
要动态定义类方法,请尝试
class Foo
LIST = %w(a b c)
class << self
LIST.each do |x|
define_method(x) do |arg|
return arg+5
end
end
end
end
Then try
然后试试
Foo.a(10)
#3
7
Instance methods of an objects singleton class are singleton methods of the object itself. So if you do
对象singleton类的实例方法是对象本身的单例方法。所以,如果你这样做
class B
def self.run(method)
singleton_class = class << self; self; end
singleton_class.send(:define_method, method) do
puts "Method #{method}"
end
end
end
you can now call
你现在可以打电话了
B.run :foo
B.foo
=> Method foo
(Edit: added B.run :foo as per Lars Haugseth's comment)
(编辑:根据Lars Haugseth的评论补充B.run:foo)
#1
1
Here's something re-worked to use class methods:
这里有一些重新使用类方法的东西:
class B
def self.before_method
puts "before method"
end
def self.run(method)
define_singleton_method(method) do
before_method
puts "method #{method}"
end
end
end
Update: Using define_singleton_method
from Ruby 1.9 which properly assigns to the eigenclass.
更新:使用Ruby 1.9中的define_singleton_method正确分配给特征类。
#2
25
To create instance methods dynamically, try
要动态创建实例方法,请尝试
class Foo
LIST = %w(a b c)
LIST.each do |x|
define_method(x) do |arg|
return arg+5
end
end
end
Now any instance of Foo would have the method "a", "b", "c". Try
现在任何Foo实例都有方法“a”,“b”,“c”。尝试
Foo.new.a(10)
To define class methods dynamically, try
要动态定义类方法,请尝试
class Foo
LIST = %w(a b c)
class << self
LIST.each do |x|
define_method(x) do |arg|
return arg+5
end
end
end
end
Then try
然后试试
Foo.a(10)
#3
7
Instance methods of an objects singleton class are singleton methods of the object itself. So if you do
对象singleton类的实例方法是对象本身的单例方法。所以,如果你这样做
class B
def self.run(method)
singleton_class = class << self; self; end
singleton_class.send(:define_method, method) do
puts "Method #{method}"
end
end
end
you can now call
你现在可以打电话了
B.run :foo
B.foo
=> Method foo
(Edit: added B.run :foo as per Lars Haugseth's comment)
(编辑:根据Lars Haugseth的评论补充B.run:foo)