Ruby way for data serialization and simple postprocessing -
Ruby way for data serialization and simple postprocessing -
i'm still new ruby. have model point , need serialized , processed info it, wrote code:
points = point.joins(:tracksegment).where('tracksegments.track_id' => track.id).to_a prev_point = nil points.each |point| if prev_point != nil @fl_time += point.point_created_at - prev_point.point_created_at @points << {:fl_time => point.point_created_at - prev_point.point_created_at, :fl_time_abs => fl_time, :elevation_diff => (prev_point.elevation - point.elevation).round(2), :elevation => point.elevation.round(2), :abs_altitude => point.abs_altitude, :latitude => point.latitude, :longitude => point.longitude, :distance => point.distance.to_i, :h_speed => point.h_speed.round(2), :v_speed => point.v_speed.round(2), :glrat => (point.h_speed.round(2) / point.v_speed.round(2)).round(2) } end prev_point = point end
is there way create more compact , create amazing (true ruby (or rails) way)?
i thought .map(&:serilizable_hash)
returns strings keys not symbols.
also thought each_proc(2) |prev, curr|
, it's kind of converting, not changing current element of array.
ruby-on-rails ruby
Comments
Post a Comment