Ruby 中的关键字在哪里定义? [英] Where are keywords defined in Ruby?

查看:55
本文介绍了Ruby 中的关键字在哪里定义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查看 Ruby 文档,想知道是否一切都是对象,那么关键字"也是对象,对吗?如果是这样,它们在 ruby​​ 中是在哪里定义的?

I was looking at the Ruby documentation, and am wondering if everything is an object then 'keywords' are objects as well, correct? And if so, where are they defined in ruby?

下面的页面让我很困惑,因为它显示了包含所有关键字的对象,但这不是所有类都使用的官方对象,这是从不同的类中以某种方式混入的吗??

The following page totally confused me caused it showed the object with all the keywords in it, however this is not the official Object that is used by all classes, is this mixed-in somehow from a different class??

http://ruby-doc.org/docs/keywords/1.9/对象.html

我猜上面有很多问题,主要的一个是:ruby关键词是如何进入ruby的?

I guess there are lots of questions above, the main one is: how do ruby keywords get into ruby?

推荐答案

关键字不是对象,而是在解析器中定义的,可以在 Ruby 源代码的 parse.y 中找到.这是该文件中的相关部分:

The keywords are not objects but defined in the parser which can be found in parse.y in the Ruby source. Here's the relevant part from that file:

reswords    : keyword__LINE__ | keyword__FILE__ | keyword__ENCODING__
        | keyword_BEGIN | keyword_END
        | keyword_alias | keyword_and | keyword_begin
        | keyword_break | keyword_case | keyword_class | keyword_def
        | keyword_defined | keyword_do | keyword_else | keyword_elsif
        | keyword_end | keyword_ensure | keyword_false
        | keyword_for | keyword_in | keyword_module | keyword_next
        | keyword_nil | keyword_not | keyword_or | keyword_redo
        | keyword_rescue | keyword_retry | keyword_return | keyword_self
        | keyword_super | keyword_then | keyword_true | keyword_undef
        | keyword_when | keyword_yield | keyword_if | keyword_unless
        | keyword_while | keyword_until
        ;

如果您想了解有关 Ruby 解析器的更多信息,请查看演示文稿 Hacking来自 RubyConf 2009 的 parse.y 或来自 Ruby 的 Parse.y famtour凯吉 2011.

If you want to know more about the Ruby parser, look at the presentation Hacking parse.y from RubyConf 2009 or Parse.y famtour from Ruby Kaigi 2011.

此外,许多随处可用的方法(例如 puts)都在 内核 模块.

Also, a lot of the methods that are available everywhere (like e.g. puts) are defined in the Kernel module.

还有一个文档中的关键词列表,感谢@antinome 指出这一点.

There's also a list of key words in the documentation, thanks @antinome for pointing that out.

这篇关于Ruby 中的关键字在哪里定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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