我还是红宝石的新手。我有模型Point,我需要序列化和处理过的数据,我写了这段代码:
points = Point.joins(:tracksegment).where('tracksegments.track_id' => track.id).to_a
prev_point = nil
points.each do |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
有没有什么方法可以让它更紧凑,让它变得惊人(真正的红宝石(或铁轨)方式)?
我考虑过.map(&:serilizable_hash)
,但它将字符串作为键而不是符号返回。
我也想过each_proc(2) do |prev, curr|
,但它是某种转换,而不仅仅是改变数组的当前元素。