加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

以编程方式获取Ruby关键字

发布时间:2020-12-17 03:32:31 所属栏目:百科 来源:网络整理
导读:我在 Ruby中有一个Array,包含所有关键字. 例如: RUBY_KEYWORDS = %w( alias and BEGIN begin break case class def defined do else elsif END end ensure false for if in module next nil not or redo rescue retry return self super then true undef un
我在 Ruby中有一个Array,包含所有关键字.

例如:

RUBY_KEYWORDS = %w(

  alias and BEGIN begin break case class def defined 
  do else elsif END end ensure false for if in module 
  next nil not or redo rescue retry return self super 
  then true undef unless until when while yield

)

我的问题很简单:

是否有以编程方式访问所有关键字的内置方式?

我的一些项目需要针对用户输入运行查询,
并且必须在中定义相同的数组有点烦人
所有这些项目.

解决方法

试试这个代码:)

RubyToken::TokenDefinitions.select { |definition| definition[1] == RubyToken::TkId }
                           .map { |definition| definition[2] }
                           .compact
                           .sort

# returns :
# ["BEGIN","END","__FILE__","__LINE__","alias","and","begin","break","case","class","def","defined?","do","else","elsif","end","ensure","false","for","if","in","module","next","nil","not","or","redo","rescue","retry","return","self","super","then","true","undef","unless","until","when","while","yield"]

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读