
Data Structure

This illustrates a data structure, a means of storing data within a program.

You may see other such structures in the Data Structures category.


Task

Create a compound data type:

  Point(x,y) 



A compound data type is one that holds multiple independent values.



Related task

   Enumeration


See also

 Array
 Associative array: Creation, Iteration
 Collections
 Compound data type
 Doubly-linked list: Definition, Element definition, Element insertion, List Traversal, Element Removal
 Linked list
 Queue: Definition, Usage
 Set
 Singly-linked list: Element definition, Element insertion, List Traversal, Element Removal
 Stack


Point = Struct.new(:x,:y)
pt = Point.new(6,7)
puts pt.x        #=> 6
pt.y = 3
puts pt          #=> #<struct Point x=6, y=3>
 
# The other way of accessing
pt = Point[2,3]
puts pt[:x]      #=> 2
pt['y'] = 5
puts pt          #=> #<struct Point x=2, y=5>
 
pt.each_pair{|member, value| puts "#{member} : #{value}"}
                 #=> x : 2
                 #=> y : 5