Created
October 21, 2011 04:34
-
-
Save nuna/1303104 to your computer and use it in GitHub Desktop.
Array of Struct object
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/ruby | |
Person = Struct.new(:name, :age) | |
jo = Person.new("Jo", 15) | |
meg = Person.new("Meg", 16) | |
beth = Person.new("Beth", 10) | |
amy = Person.new("Amy", 7) | |
sisters = [jo, meg, beth, amy] | |
sisters.each{|person| puts person.name } | |
#=> Jo | |
#=> Meg | |
#=> Beth | |
#=> Amy | |
Item = Struct.new(:price) | |
items = [] | |
5.times do | |
items << Item.new(rand(1000)) | |
end | |
items.each{|i| puts i.price } | |
#=> 961 | |
#=> 685 | |
#=> 538 | |
#=> 848 | |
#=> 654 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment