# 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
case/when. Previously a colon could be used as a shorthand for a
thenstatement; this is perhaps most useful with multiple
whenstatements 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' endBut 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