我可以在 rails 模型中定义_方法吗? [英] Can I define_method in rails models?

查看:39
本文介绍了我可以在 rails 模型中定义_方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 rails 模型有代码试图在模型内部 define_method(method_name).

My rails model has code that is attempting to define_method(method_name) inside the model.

我不断得到:

NoMethodError: undefined method `define_method'

我做错了什么?我是不是在错误的地方做这件事.我需要将此方法附加到此模型.我还能在哪里定义这个方法?

What am I doing wrong? Am I doing this in the wrong place. I need this method attached to this model. Where else can I define this method?

对于那些要求查看代码的人:

For those asking to see the code:

for field in rdev_fields
  next if self.attributes.include?(field)
  count = count + 1
  rdev_hash[field.to_sym] = self.attributes["attribute#{count}"]
  if !self.respond_to?(field) then
    define_method("#{field}") do
      self.send("attribute#{count}".to_sym)
    end
  end
end

推荐答案

rails 模型没有什么神奇之处,它只是一个普通的类,带有一堆预先存在的方法,

There's nothing magical or about a rails model, it's just a normal class with a bunch of pre-existing methods,

那么,问题是我可以在类中定义方法吗"?

So, the question is "can I define_method in a class"?

重要的区别在于您可以在类中定义方法而不是在实例中方法

The important distinction is than you can define method in a class not in an instance method

例如:

class Cow
  define_method "speak" do
    "MOOOO"
  end
end

Cow.new.speak
=> "MOOOO"

这应该可以正常工作.请注意,您是在类 Cow 上定义它的,因此您已经拥有的任何其他 Cows 都会自动添加该方法.

This should work fine. Note you're defining it on the class Cow, so any other Cows that you already have will automatically get that method added.

您不能从实例方法定义方法,因此您必须获取类,并使用它来定义方法.像这样:

You can't define methods from an instance method, so you have to grab the class, and use that to define the method. Like this:

class Cow
  def add_speak
    self.class.send(:define_method, :speak) do
      "MOOOO added"
    end
  end
end

Cow.new.speak
NoMethodError: undefined method 'speak' for #<Cow:0xb7c48530>

Cow.new.add_speak
Cow.new.speak
=> "MOOOO added"

问题解决了.细心的读者会注意到,在这个例子中我使用了 send(:define_method) - 这是必需的,因为 define_method 是私有的,并且私有方法只能被它们访问'在.在这种情况下,define_method在类中,我们在实例中,所以我们不能直接访问它.

Problem solved. Astute readers will note that in this example I'm using send(:define_method) - this is needed because define_method is private, and private methods are only accessible to the thing they're in. In this case, define_method is in the class, we are in the instance, so we can't directly access it.

尽管如此,我们将方法直接添加到类中,因此所有其他已经存在的 Cows 也会自动添加 speak 方法.

As above though, we're adding the method directly to the class, so all other Cows which already exist will automatically also get the speak method added.

示例:

class Cow
  def add_speak_just_me
    class << self
      define_method "speak" do
        "MOOOO added for just me"
      end
    end
  end
end

Cow.new.speak
NoMethodError: undefined method 'speak' for #<Cow:0xb7c72b78>

c = Cow.new
c.add_speak_just_me
c.speak
=> "MOOOO added for just me" # it works, hooray

Cow.new.speak # this new cow doesn't have the method, it hasn't been automatically added
NoMethodError: undefined method `speak' for #<Cow:0xb7c65b1c>

这是如何工作的?你去的兔子洞!

How does this work? Down the rabbithole you go!

阅读:http://dannytatom.me/metaid/,祝你好运.当您意识到向实例添加方法"实际上根本没有将其添加到实例时,它会有所帮助:-)

Read this: http://dannytatom.me/metaid/ and good luck. It helps when you realise that 'adding a method' to an instance isn't actually adding it to the instance at all :-)

这篇关于我可以在 rails 模型中定义_方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆