The correct Ruby framework for this implementation

I would like to know about any data structure in Ruby that allows us to conveniently store pairs of numbers.

I would like to store pairs of numbers, like coordinates, in a list without regard to order.

Like it so much [(0,0), (0,1), ... (x,y)]

If there are no data structures that will do this, what can I achieve closest with something else?

Thank.

+3
source share
2 answers

You can use a nested array, for example:

 array = [[0,0], [0,1], ... [x,y]]
+2
source

Another way is Struct to determine Pair.

Pair = Struct.new(:x, :y) do
  def to_s
    "(#{x}, #{y})"
  end
end

Then yon can use it as other built-in data structures. [Pair.new(0,0), Pair.new(1,1)].

, Pair , Pair.

def Pair(x, y)
  Pair.new(x, y)
end

, [Pair(0,0), Pair(1,1)]

+3

All Articles