The following keywords have special meaning in Ruby and are treated specially by the Ruby parser:

__LINE__      case         ensure       not          then
__ENCODING__  class        false        or           true
__FILE__      def          for          redo         undef
BEGIN         defined?     if           rescue       unless
END           do           in           retry        until
alias         else         module       return       when
and           elsif        next         self         while
begin         end          nil          super        yield
break

In addition to those keywords, there are three keyword-like tokens that are treated specially by the Ruby parser when they appear at the beginning of a line:

=begin    =end      __END__

As we’ve seen, =begin and =end at the beginning of a line delimit multiline comments. And the token __END__ marks the end of the program (and the beginning of a data section) if it appears on a line by itself with no leading or trailing white space.

In most languages, these words would be called “reserved words” and they would be never allowed as identifiers. The Ruby parser is flexible and does not complain if you prefix these keywords with @, @@, or $ prefixes and use them as instance, class, or global variable names. Also, you can use these keywords as method names, with the caveat that the method must always be explicitly invoked through an object. Note, however, that using these keywords in identifiers will result in confusing code. The best practice is to treat these keywords as reserved.

Many important features of the Ruby language are actually implemented as methods of the Kernel, Module, Class, and Object classes. It is good practice, therefore, to treat the following identifiers as reserved words as well:

# These are methods that appear to be statements or keywords
at_exit        catch          private        require
attr           include        proc           throw
attr_accessor  lambda         protected
attr_reader    load           public
attr_writer    loop           raise

# These are commonly used global functions
Array          chomp!         gsub!          select
Float          chop           iterator?      sleep
Integer        chop!          load           split
String         eval           open           sprintf
URI            exec           p              srand
abort          exit           print          sub
autoload       exit!          printf         sub!
autoload?      fail           putc           syscall
binding        fork           puts           system
block_given?   format         rand           test
callcc         getc           readline       trap
caller         gets           readlines      warn
chomp          gsub           scan

# These are commonly used object methods
allocate       freeze         kind_of?       superclass
clone          frozen?        method         taint
display        hash           methods        tainted?
dup            id             new            to_a
enum_for       inherited      nil?           to_enum
eql?           inspect        object_id      to_s
equal?         instance_of?   respond_to?    untaint
extend         is_a?          send
Advertisements