Where Are Keywords Defined in Ruby

Where are keywords defined in Ruby?

The keywords are not objects but defined in the parser which can be found in parse.y in the Ruby source. Here's the relevant part from that file:

reswords    : keyword__LINE__ | keyword__FILE__ | keyword__ENCODING__
| keyword_BEGIN | keyword_END
| keyword_alias | keyword_and | keyword_begin
| keyword_break | keyword_case | keyword_class | keyword_def
| keyword_defined | keyword_do | keyword_else | keyword_elsif
| keyword_end | keyword_ensure | keyword_false
| keyword_for | keyword_in | keyword_module | keyword_next
| keyword_nil | keyword_not | keyword_or | keyword_redo
| keyword_rescue | keyword_retry | keyword_return | keyword_self
| keyword_super | keyword_then | keyword_true | keyword_undef
| keyword_when | keyword_yield | keyword_if | keyword_unless
| keyword_while | keyword_until
;

If you want to know more about the Ruby parser, look at the presentation Hacking parse.y from RubyConf 2009 or Parse.y famtour from Ruby Kaigi 2011.

Also, a lot of the methods that are available everywhere (like e.g. puts) are defined in the Kernel module.

EDIT: There's also a list of key words in the documentation, thanks @antinome for pointing that out.

What is the meaning of the keyword do, in ruby?

do ... end (or alternatively { ... }) creates a so-called block, which is a type of anonymous function in Ruby. In your example that block is passed as an argument to group. group then does some bookkeeping to set the given groups as active, executes the block, and then deactivates the groups again.

Programmatic way to obtain Ruby keywords

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

Where in the Ruby language is %q, %w, etc., defined?

They are “hard coded” in the parser; see

  • parse.y from the tip of Ruby 1.9.2 or
  • parse.y from the tip of Ruby 1.8.7.

The easiest way to find the code in question is to look for the second occurrence of str_sword (Single-quoted WORDs). All the “delimited input” syntax is defined there: %Q, %q, %W, %w, %x, %r, and %s (both versions referenced above define the same set of delimited input markers).

What does this kind of keyword use do in Ruby?

layout isn't a keyword. layout false is the same as layout(false). The parentheses are optional for method calls in Ruby.

See the layout documentation for details of the layout() method in Rails.

Metaprogrammatically defining Ruby methods that take keyword arguments?

I wound up using a (surprisingly Pythonic) **kwargs strategy, thanks to the new features in Ruby 2.0+:

module StricterStruct
def self.new(*attribute_names_as_symbols)
c = Class.new
l = attribute_names_as_symbols

c.instance_eval {
define_method(:initialize) do |**kwargs|
unless kwargs.keys.sort == l.sort
extra = kwargs.keys - l
missing = l - kwargs.keys

raise ArgumentError.new <<-MESSAGE
keys do not match expected list:
-- missing keys: #{missing}
-- extra keys: #{extra}
MESSAGE
end

kwargs.map do |k, v|
instance_variable_set "@#{k}", v
end
end

l.each do |sym|
attr_reader sym
end
}

c
end
end

Built-in way to determine whether a string is a Ruby reserved word?

The only way I can think of is loading an array with all the keywords you know about.

class String
def is_keyword?
%w{__FILE__ __LINE__ 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}.include? self
end
end
"foobar".is_keyword? # => false
"for".is_keyword? # => true

For reference:

  • I know this isn't a built-in way, it's just the only way I could think of. Don't downvote me for it.
  • The list I included is that of true keywords. public, protected and friends aren't really keywords, just the names of important methods which are called during the creation of modules or classes. You can think of it as elements of the Ruby DSL.


Related Topics



Leave a reply



Submit