Subsecciones

Acceso a Arrays y a Hashes

Véase el código en la sección 14.1.6
# Define [] method to allow a Point to look like an array or
# a hash with keys :x and :y
def [](index)
  case index
  when 0, -2: @x         # Index 0 (or -2) is the X coordinate
  when 1, -1: @y         # Index 1 (or -1) is the Y coordinate
  when :x, "x": @x       # Hash keys as symbol or string for X
  when :y, "y": @y       # Hash keys as symbol or string for Y
  else nil               # Arrays and hashes just return nil on bad indexes
  end
end

Nota Sobre el Case

Ruby 1.9 introduces an incompatible syntax change for conditional statements such as if and case/when. Previously a colon could be used as a shorthand for a then statement; this is perhaps most useful with multiple when statements on one line.

The following is legitimate ruby in 1.8:

case x 
when Regexp  : puts 'a regex'
when Hash    : puts 'a regex'
when Numeric : puts 'a number'
when String  : puts 'a string'
end
But not in ruby 1.9; now an explicit 'then' statement must be used:

case x
when Regexp then puts 'a regex'
...

Casiano Rodriguez León 2015-01-07