Parent

Methods

Class Index [+]

Quicksearch

Ramaze::Struct

Public Instance Methods

values_at(*keys) click to toggle source

Makes it possible to access the keys directly by their name, not only their index. The original Struct#values_at tries to call # on the arguments. in Ruby < 1.9 this would lead to a quite unintuitive IndexError, while in 1.9 it will say outright that it cannot convert Symbol into Integer. I think this is neither useful nor expected, so we extend Struct#values_at to recognize valid members of the Struct passed into the method and return their values in the same order, just like Hash#values_at.

Example:

 Point = Struct.new(:x, :y)
 point = Point.new(15, 10)

 # Old behaviour:
 point.values_at(0, 1)
 # => [15, 10]
 point.values_at(0..1)
 # => [15, 10]
 point.values_at(:y, :x)
 # => IndexError: offset 20697 too large for struct(size:2)

 # Added new behaviour:
 point.values_at(:y, :x)
 # => [10, 15]
    # File lib/ramaze/snippets/ramaze/struct.rb, line 32
32:     def values_at(*keys)
33:       keys.map do |key|
34:         case key
35:         when String, Symbol
36:           self[key]
37:         else
38:           return super
39:         end
40:       end
41:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.