instance method
Struct#[]
(::Struct::index name_or_position) -> Elem
struct[name] → object
struct[n] → object
struct.c
View on GitHub ↗
VALUE
rb_struct_aref(VALUE s, VALUE idx)
{
int i = rb_struct_pos(s, &idx);
if (i < 0) invalid_struct_pos(s, idx);
return RSTRUCT_GET(s, i);
}
Returns a value from self.
With symbol or string argument name given, returns the value for the named member:
Example
Customer = Struct.new(:name, :address, :zip) joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345) joe[:zip] # => 12345
Raises NameError if name is not the name of a member.
With integer argument n given, returns self.values[n] if n is in range; see Array Indexes at Array:
Example
joe[2] # => 12345 joe[-2] # => "123 Maple, Anytown NC"
Raises IndexError if n is out of range.