Ruby 中的方法:对象与否? [英] Methods in Ruby: objects or not?
问题描述
受到
Inspired by this discussion, after some googling I wasn't able to find an answer to a pretty simple question regarding methods in Ruby: are methods objects or not?
There are different opinions here and there, and I would really like to hear, let's say, an in-depth explanation.
I'm aware of Object#method
method, which takes a method name and returns a Method
instance, but, on the other hand, there's a similar thing you can do with blocks to make them into Proc
instances, and blocks aren't objects, so what makes methods any different?
Methods are a fundamental part of Ruby's syntax, but they are not values that Ruby programs can operate on. That is, Ruby's methods are not objects in the way that strings, numbers, and arrays are. It is possible, however, to obtain a Method object that represents a given method, and we can invoke methods indirectly through Method objects.
From The Ruby Programming Language:
这篇关于Ruby 中的方法:对象与否?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!