gpt4 book ai didi

ruby - 编程方式获取Ruby关键字

转载 作者:数据小太阳 更新时间:2023-10-29 07:14:16 25 4
gpt4 key购买 nike

我有一个 Ruby 数组,包含所有关键字。

例如:

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

关于ruby - 编程方式获取Ruby关键字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27771439/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com