Where are keywords defined in Ruby?

前端 未结 1 1291
陌清茗
陌清茗 2021-02-14 05:11

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?<

1条回答
  •  梦谈多话
    2021-02-14 06:07

    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
            ;
    

    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.

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

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

    0 讨论(0)
提交回复
热议问题