Skip to content

Instantly share code, notes, and snippets.

@nuna
Created October 21, 2011 04:34
Show Gist options
  • Save nuna/1303104 to your computer and use it in GitHub Desktop.
Save nuna/1303104 to your computer and use it in GitHub Desktop.
Array of Struct object
#!/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