给定一个类,查看实例是否有方法 (Ruby)
我知道在 Ruby 中我可以使用 respond_to?
来检查对象是否具有某种方法。
但是,给定类,我如何检查实例是否具有某种方法?
即,类似
Foo.new.respond_to?(:bar)
但我觉得必须有比实例化新实例更好的方法。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
我不知道为什么每个人都建议当
method_define?
完成这项工作时,您应该使用instance_methods
和include?
。注意
如果您从另一种面向对象语言转向 Ruby,或者您认为
method_define
只表示您显式定义的方法:那么请阅读以下内容:
在 Ruby 中,属性 (模型上的属性)基本上也是一种方法。因此,
method_define?
也将为属性返回 true,不仅仅是方法。例如:
给定一个具有 String 属性的类实例 < code>first_name:
因为
first_name
既是属性又是方法(以及 String 类型的字符串)。I don't know why everyone is suggesting you should be using
instance_methods
andinclude?
whenmethod_defined?
does the job.NOTE
In case you are coming to Ruby from another OO language OR you think that
method_defined
means ONLY methods that you defined explicitly with:then read this:
In Ruby, a property (attribute) on your model is basically a method also. So
method_defined?
will also return true for properties, not just methods.For example:
Given an instance of a class that has a String attribute
first_name
:since
first_name
is both an attribute and a method (and a string of type String).您可以按如下方式使用
method_define?
:比其他人似乎都建议的
instance_methods.include?
更容易、可移植且高效。请记住,您不会知道类是否通过
method_missing
动态响应某些调用,例如通过重新定义respond_to?
,或者从 Ruby 1.9.2 开始通过定义 < a href="http://github.com/ruby/ruby/blob/trunk/vm_method.c#L1288" rel="noreferrer">respond_to_missing?
。You can use
method_defined?
as follows:Much easier, portable and efficient than the
instance_methods.include?
everyone else seems to be suggesting.Keep in mind that you won't know if a class responds dynamically to some calls with
method_missing
, for example by redefiningrespond_to?
, or since Ruby 1.9.2 by definingrespond_to_missing?
.实际上这对对象和类都不起作用。
确实如此:
因此,根据给定的答案,这有效:
但这不起作用:
所以我将其用于类和对象:
类:
对象:
Actually this doesn't work for both Objects and Classes.
This does:
So with the given answer, this works:
But this does NOT work:
So I use this for both classes and objects:
Classes:
Objects:
“给出一个类,看看是否实例有方法 (Ruby)” 更好。显然 Ruby 有这个内置功能,但我不知何故错过了它。我的回答仅供参考,无论如何。
Ruby 类响应方法
instance_methods
和public_instance_methods
。在 Ruby 1.8 中,第一个在字符串数组中列出所有实例方法名称,第二个将其限制为公共方法。第二种行为是您最可能想要的,因为respond_to?
默认情况下也将自身限制为公共方法。但在 Ruby 1.9 中,这些方法返回符号数组。
如果您计划经常这样做,您可能需要扩展
Module
以包含快捷方法。 (将其分配给Module
而不是Class
可能看起来很奇怪,但由于这是instance_methods
方法所在的位置,因此最好保持一致)如果您想同时支持 Ruby 1.8 和 Ruby 1.9,那么这也是添加搜索字符串和符号的逻辑的方便位置。
The answer to "Given a class, see if instance has method (Ruby)" is better. Apparently Ruby has this built-in, and I somehow missed it. My answer is left for reference, regardless.
Ruby classes respond to the methods
instance_methods
andpublic_instance_methods
. In Ruby 1.8, the first lists all instance method names in an array of strings, and the second restricts it to public methods. The second behavior is what you'd most likely want, sincerespond_to?
restricts itself to public methods by default, as well.In Ruby 1.9, though, those methods return arrays of symbols.
If you're planning on doing this often, you might want to extend
Module
to include a shortcut method. (It may seem odd to assign this toModule
instead ofClass
, but since that's where theinstance_methods
methods live, it's best to keep in line with that pattern.)If you want to support both Ruby 1.8 and Ruby 1.9, that would be a convenient place to add the logic to search for both strings and symbols, as well.
尝试 Foo.instance_methods.include 吗? :栏
Try
Foo.instance_methods.include? :bar
不确定这是否是最好的方法,但您始终可以这样做:
Not sure if this is the best way, but you could always do this:
我认为 Rails 中的
method_define?
有问题。它可能不一致或其他什么,所以如果你使用 Rails,最好使用attribute_method?(属性)
。“在 ActiveRecord 类上测试 method_define?在实例化之前不起作用”是关于不一致的问题。
I think there is something wrong with
method_defined?
in Rails. It may be inconsistent or something, so if you use Rails, it's better to use something fromattribute_method?(attribute)
."testing for method_defined? on ActiveRecord classes doesn't work until an instantiation" is a question about the inconsistency.
如果您要检查一个对象是否可以响应一系列方法,您可以执行以下操作:
methods & some.methods
将在两个数组的公共/匹配元素上连接它们。 some.methods 包括您正在检查的所有方法,它等于方法。例如:所以
在这种情况下,您需要使用符号,因为当您调用 .methods 时,它会返回符号数组,并且如果您使用
["my", "methods"]
,它会返回 false。If you're checking to see if an object can respond to a series of methods, you could do something like:
the
methods & something.methods
will join the two arrays on their common/matching elements. something.methods includes all of the methods you're checking for, it'll equal methods. For example:so
In this situation, you'd want to use symbols, because when you call .methods, it returns an array of symbols and if you used
["my", "methods"]
, it'd return false.希望这对您有帮助!
Hope this helps you!
klass.instance_methods.include :method_name
或"method_name"
,具体取决于我认为的 Ruby 版本。klass.instance_methods.include :method_name
or"method_name"
, depending on the Ruby version I think.虽然
respond_to?
仅对公共方法返回 true,但检查类上的“方法定义”也可能与私有方法有关。在 Ruby v2.0+ 上,可以通过以下方式检查公共集和私有集:
While
respond_to?
will return true only for public methods, checking for "method definition" on a class may also pertain to private methods.On Ruby v2.0+ checking both public and private sets can be achieved with
在我使用 ruby 2.5.3 的情况下,以下句子完美运行:
它将返回布尔值 true 或 false。
On my case working with ruby 2.5.3 the following sentences have worked perfectly :
It will return a boolean value true or false.