instance methods
|
==
|
CustomerInst == anOtherStruct
-> true or false
|
|
Equality---Returns 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
|
|
[ ]
|
CustomerInst[ aSymbol ] -> anObject
CustomerInst[ anInteger ] -> anObject
|
|
Attribute Reference---Returns the value of the instance variable
named by aSymbol, or indexed (0..length-1) by
anInteger. Will raise 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"
|
|
[ ]=
|
CustomerInst[ aSymbol ] = anObject -> anObject
CustomerInst[ anInteger ] = anObject -> anObject
|
|
Attribute Assignment---Assigns to the instance variable named by
aSymbol or anInteger
the value anObject and returns it. Will raise
a 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"
|
|
each
|
CustomerInst.each {| anObject | block }
-> CustomerInst
|
|
Calls block once for each instance variable, passing the
value as a parameter.
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
|
|
length
|
CustomerInst.length
-> anInteger
|
|
Returns the number of instance variables.
Customer = Struct.new( "Customer", :name, :address, :zip )
|
joe = Customer.new( "Joe Smith", "123 Maple, Anytown NC", 12345 )
|
joe.length
|
� |
3
|
|
members
|
CustomerInst.members
-> anArray
|
|
Returns an array of strings representing the names of the
instance variables.
Customer = Struct.new( "Customer", :name, :address, :zip )
|
joe = Customer.new( "Joe Smith", "123 Maple, Anytown NC", 12345 )
|
joe.members
|
� |
["name", "address", "zip"]
|
|
size
|
CustomerInst.size
-> anInteger
|
|
Synonym for
Struct#length
.
|
to_a
|
CustomerInst.to_a
-> anArray
|
|
Returns the values for this instance as an array.
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"
|
|
values
|
CustomerInst.values
-> anArray
|
|
Synonym for to_a .
|