Subclasses: Struct::Tms
A Struct
is a convenient way to bundle a number of attributes together, using accessor methods, without having to write an explicit class.
The Struct
class is a generator of specific classes, each one of which is defined to hold a set of variables and their accessors. In these examples, we'll call the generated class “CustomerClass,” and we'll show an example instance of that class as “CustomerInst.”
In the descriptions that follow, the parameter aSymbol refers to a symbol, which is either a quoted string or a Symbol
(such as :name
).
Enumerable
collect
, detect
, each_with_index
, entries
, find
, find_all
, grep
, include?
, map
, max
, member?
, min
, reject
, select
, sort
, to_a
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 Struct
s in the system and should start with a capital letter.
Struct.new
returns a new Class
object, which can then be used to create specific instances of the new structure. The remaining methods listed below (class and instance) are defined for this generated class. See the description that follows for an example.
nil
. Passing too many parameters will raise an ArgumentError
.
Customer = Struct.new( "Customer", :name, :address, :zip )
joe = Customer.new( "Joe Smith", "123 Maple, Anytown NC", 12345 )
joe.name → "Joe Smith"
joe.zip → 12345
Customer = Struct.new( "Customer", :name, :address, :zip )
Customer.members → ["name", "address", "zip"]
true
or false
true
if anOtherStruct is equal to this one: they must be of the same class as generated by Struct.new
, and the values of all instance variables must be equal (according to Object#==
).
Customer = Struct.new( "Customer", :name, :address, :zip )
joe = Customer.new( "Joe Smith", "123 Maple, Anytown NC", 12345 )
joejr = Customer.new( "Joe Smith", "123 Maple, Anytown NC", 12345 )
jane = Customer.new( "Jane Doe", "456 Elm, Anytown NC", 12345 )
joe == joejr → true
joe == jane → false
NameError
if the named variable does not exist, or IndexError
if the index is out of range.
Customer = Struct.new( "Customer", :name, :address, :zip )
joe = Customer.new( "Joe Smith", "123 Maple, Anytown NC", 12345 )
joe["name"] → "Joe Smith"
joe[:name] → "Joe Smith"
joe[0] → "Joe Smith"
NameError
if the named variable does not exist, or an IndexError
if the index is out of range.
Customer = Struct.new( "Customer", :name, :address, :zip )
joe = Customer.new( "Joe Smith", "123 Maple, Anytown NC", 12345 )
joe["name"] = "Luke"
joe[:zip] = "90210"
joe.name → "Luke"
joe.zip → "90210"
Customer = Struct.new( "Customer", :name, :address, :zip )
joe = Customer.new( "Joe Smith", "123 Maple, Anytown NC", 12345 )
joe.each {|x| puts(x) }
produces:
Joe Smith
123 Maple, Anytown NC
12345
Customer = Struct.new( "Customer", :name, :address, :zip )
joe = Customer.new( "Joe Smith", "123 Maple, Anytown NC", 12345 )
joe.length → 3
Customer = Struct.new( "Customer", :name, :address, :zip )
joe = Customer.new( "Joe Smith", "123 Maple, Anytown NC", 12345 )
joe.members → ["name", "address", "zip"]
Struct#length
.
Customer = Struct.new( "Customer", :name, :address, :zip )
joe = Customer.new( "Joe Smith", "123 Maple, Anytown NC", 12345 )
joe.to_a[1] → "123 Maple, Anytown NC"
to_a
.Extracted from the book "Programming Ruby - The Pragmatic Programmer's Guide"
Copyright © 2001 by Addison Wesley Longman, Inc. This material may be distributed only subject to the terms and conditions set forth in the Open Publication License, v1.0 or later (the latest version is presently available at http://www.opencontent.org/openpub/).
Distribution of substantively modified versions of this document is prohibited without the explicit permission of the copyright holder.
Distribution of the work or derivative of the work in any standard (paper) book form is prohibited unless prior permission is obtained from the copyright holder.