In [50]:
require "json"


Out[50]:
true

In [38]:
class Point
  def initialize(x, y)
    @x = x
    @y = y
  end
  
  def x
    @x
  end
  
  def y
    @y
  end
  
  def to_s
    "(#@x,#@y)"
  end
end


Out[38]:
:to_s

In [46]:
path = [Point.new(0,1), Point.new(5,4), Point.new(2,8)]


Out[46]:
[#<Point:0x007ff08c8da9a8 @x=0, @y=1>, #<Point:0x007ff08c8da980 @x=5, @y=4>, #<Point:0x007ff08c8da958 @x=2, @y=8>]

In [49]:
def calculate_points(path)
  puts path.length
#   path.each do |p|
#      puts p.to_s 
#   end
  
  for i in 1..(path.length-1)
    puts "#{i} : #{path[i-1].x - path[i].x}"
    
  end
end

calculate_points(path)


3
1 : -5
2 : 3
Out[49]:
1..2

In [52]:
x = Point.new(0,0).to_json


Out[52]:
"\"(0,0)\""

In [59]:
a = path.map{|pt| {x: pt.x, y: pt.y, label: pt.to_s}}
puts a.to_json


[{"x":0,"y":1,"label":"(0,1)"},{"x":5,"y":4,"label":"(5,4)"},{"x":2,"y":8,"label":"(2,8)"}]

In [ ]: