以编程方式获取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"] (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |