Ruby中的方法:对象还是没有对象? [英] Methods in Ruby: objects or not?

查看:80
本文介绍了Ruby中的方法:对象还是没有对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此讨论的启发,经过一番谷歌搜索之后,我不是找不到关于Ruby中方法的一个非常简单的问题的答案:方法对象是否存在?

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.

我知道Object#method方法,该方法带有一个方法名称并返回一个Method实例,但是,另一方面,您可以对块执行类似的操作以使它们成为Proc实例,而块不是对象,那么什么使方法与众不同?

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?

推荐答案

方法是 Ruby的语法,但它们不是值 Ruby程序可以在上面运行的代码. 也就是说, Ruby的方法不是 对象,以字符串的方式 数字和数组.它是 但是,有可能获得一种方法 代表给定方法的对象, 我们可以间接调用方法 通过Method对象.

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.

来自 Ruby编程语言:

这篇关于Ruby中的方法:对象还是没有对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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