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