热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

以编程方式获取Ruby关键字

如何解决《以编程方式获取Ruby关键字》经验,为你挑选了1个好方法。

我在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

)

我的问题很简单:

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

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



1> romainsalles..:

试试这个代码:)

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"]


推荐阅读
author-avatar
吃货程序猿
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有