作者:吃货程序猿 | 来源:互联网 | 2023-05-28 13:59
我在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"]