I have an Array in Ruby, with all keywords.
For instance:
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
)
My question is simple:
Is there an in-built way to programmatically access all keywords?
Some of my projects need to run a query against user input, and it's a bit annoying to have to define the same array in all these projects.
Try this code :)
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"]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With