Creates a new class, named by aString, containing accessor methods for the
given symbols. If the name aString is
omitted, an anonymous structure class will be created. Otherwise, the name
of this struct will appear as a constant in class Struct, so it must be unique for all
Structs in the system and should start with a capital letter.
Assigning a structure class to a constant effectively gives the class the
name of the constant.
Struct::new returns a newClass object, which can then be used to
create specific instances of the new
structure. The number of actual parameters must be less than or equal to
the number of attributes defined for this class; unset parameters default
to \nil{}. Passing too many parameters will raise an \E{ArgumentError}.
The remaining methods listed in this section (class and instance) are
defined for this generated class.
# Create a structure with a name in StructStruct.new("Customer",:name,:address)#=> Struct::CustomerStruct::Customer.new("Dave","123 Main")#=> #<Struct::Customer name="Dave", address="123 Main"># Create a structure named by its constantCustomer=Struct.new(:name,:address)#=> CustomerCustomer.new("Dave","123 Main")#=> #<Customer name="Dave", address="123 Main">
/*
* call-seq:
* Struct.new( [aString] [, aSym]+> ) => StructClass
* StructClass.new(arg, ...) => obj
* StructClass[arg, ...] => obj
*
* Creates a new class, named by <i>aString</i>, containing accessor
* methods for the given symbols. If the name <i>aString</i> is
* omitted, an anonymous structure class will be created. Otherwise,
* the name of this struct will appear as a constant in class
* <code>Struct</code>, so it must be unique for all
* <code>Struct</code>s in the system and should start with a capital
* letter. Assigning a structure class to a constant effectively gives
* the class the name of the constant.
*
* <code>Struct::new</code> returns a new <code>Class</code> object,
* which can then be used to create specific instances of the new
* structure. The number of actual parameters must be
* less than or equal to the number of attributes defined for this
* class; unset parameters default to \nil{}. Passing too many
* parameters will raise an \E{ArgumentError}.
*
* The remaining methods listed in this section (class and instance)
* are defined for this generated class.
*
* # Create a structure with a name in Struct
* Struct.new("Customer", :name, :address) #=> Struct::Customer
* Struct::Customer.new("Dave", "123 Main") #=> #<Struct::Customer name="Dave", address="123 Main">
*
* # Create a structure named by its constant
* Customer = Struct.new(:name, :address) #=> Customer
* Customer.new("Dave", "123 Main") #=> #<Customer name="Dave", address="123 Main">
*/
static VALUE
rb_struct_s_def(argc, argv, klass)
int argc;
VALUE *argv;
VALUE klass;
{
VALUE name, rest;
long i;
VALUE st;
ID id;
rb_scan_args(argc, argv, "1*", &name, &rest);
if (!NIL_P(name) && SYMBOL_P(name)) {
rb_ary_unshift(rest, name);
name = Qnil;
}
for (i=0; i<RARRAY(rest)->len; i++) {
id = rb_to_id(RARRAY(rest)->ptr[i]);
RARRAY(rest)->ptr[i] = ID2SYM(id);
}
st = make_struct(name, rest, klass);
if (rb_block_given_p()) {
rb_mod_module_eval(0, 0, st);
}
return st;
}
1Note
Also takes a block
rjspotter ยท Jul 31, 2015
You can define methods within a block
User = Struct.new(:first_name, :last_name) do
def full_name
"#{first_name} #{last_name}"
end
end
user = User.new('Simon', 'Templar') # => #<struct User first_name="Simon", last_name="Templar">
user.full_name # => "Simon Templar"