我可以在 Ruby 中组合选择吗? (从嵌套哈希中检索信息)
Can I combine selects in Ruby? (retrieve information from nested hashes)
目前我正准备学习这门很棒的语言,想为在线游戏制作一个小型计算器"Eve Online"。
我正在努力处理这部分代码
orders = Hash.new
orders = {typeid: "type1", value: {order1: {:stationName=>"Jita IV - Moon 4 - Caldari Navy Assembly Plant", price: 3599.99, volRemain: 28},
order2: {:stationName=>"Jita IV - Moon 4 - Caldari Navy Assembly Plant", price: 3600.00, volRemain: 13}}}
{typeid: "type2", value: {order3: {:stationName=>"Jita IV - Moon 4 - Caldari Navy Assembly Plant", price: 3500.00, volRemain: 43}}}
p orders.select {|key, value| value[:order1][:price].to_i < 3600}
显然 "p orders.select" 不起作用。
我想要实现的是检索某个 typeid 的 10 个最便宜的价格。
我喜欢这里给出的方法:How do I search within an array of hashes by hash values in ruby?
然而,这迫使我将哈希值保存在一个数组中,然后又一次,我无法嵌套它们。
我不想做的是嵌套 3 个“.each do |key, value|”,因为(我想至少)它会导致 O(n^3) 的复杂性,这应该挺差的。
那么有没有办法以智能方式检索特定类型的所有 :price - 值?
先谢谢大家了!
我会使用数组:
orders = [
{ type_id: "type1", price: 3599.99, vol_remain: 28, station_name: "Jita IV - Moon 4 - Caldari Navy Assembly Plant" },
{ type_id: "type1", price: 3600.00, vol_remain: 13, station_name: "Jita IV - Moon 4 - Caldari Navy Assembly Plant" },
{ type_id: "type2", price: 3500.00, volRemain: 43, station_name: "Jita IV - Moon 4 - Caldari Navy Assembly Plant" }
]
orders.select { |order| order[:price] < 3600 }
#=> [
# {:type_id=>"type1", :price=>3599.99, :vol_remain=>28, :station_name=>"Jita IV - Moon 4 - Caldari Navy Assembly Plant"},
# {:type_id=>"type2", :price=>3500.0, :vol_remain =>43, :station_name=>"Jita IV - Moon 4 - Caldari Navy Assembly Plant"}
# ]
由于您在 Rails 上使用 Ruby,因此您应该使用模型和关联,例如:
class Order < ActiveRecord::Base
belongs_to :station
end
class Station < ActiveRecord::Base
has_many :orders
end
目前我正准备学习这门很棒的语言,想为在线游戏制作一个小型计算器"Eve Online"。
我正在努力处理这部分代码
orders = Hash.new
orders = {typeid: "type1", value: {order1: {:stationName=>"Jita IV - Moon 4 - Caldari Navy Assembly Plant", price: 3599.99, volRemain: 28},
order2: {:stationName=>"Jita IV - Moon 4 - Caldari Navy Assembly Plant", price: 3600.00, volRemain: 13}}}
{typeid: "type2", value: {order3: {:stationName=>"Jita IV - Moon 4 - Caldari Navy Assembly Plant", price: 3500.00, volRemain: 43}}}
p orders.select {|key, value| value[:order1][:price].to_i < 3600}
显然 "p orders.select" 不起作用。
我想要实现的是检索某个 typeid 的 10 个最便宜的价格。
我喜欢这里给出的方法:How do I search within an array of hashes by hash values in ruby?
然而,这迫使我将哈希值保存在一个数组中,然后又一次,我无法嵌套它们。
我不想做的是嵌套 3 个“.each do |key, value|”,因为(我想至少)它会导致 O(n^3) 的复杂性,这应该挺差的。
那么有没有办法以智能方式检索特定类型的所有 :price - 值?
先谢谢大家了!
我会使用数组:
orders = [
{ type_id: "type1", price: 3599.99, vol_remain: 28, station_name: "Jita IV - Moon 4 - Caldari Navy Assembly Plant" },
{ type_id: "type1", price: 3600.00, vol_remain: 13, station_name: "Jita IV - Moon 4 - Caldari Navy Assembly Plant" },
{ type_id: "type2", price: 3500.00, volRemain: 43, station_name: "Jita IV - Moon 4 - Caldari Navy Assembly Plant" }
]
orders.select { |order| order[:price] < 3600 }
#=> [
# {:type_id=>"type1", :price=>3599.99, :vol_remain=>28, :station_name=>"Jita IV - Moon 4 - Caldari Navy Assembly Plant"},
# {:type_id=>"type2", :price=>3500.0, :vol_remain =>43, :station_name=>"Jita IV - Moon 4 - Caldari Navy Assembly Plant"}
# ]
由于您在 Rails 上使用 Ruby,因此您应该使用模型和关联,例如:
class Order < ActiveRecord::Base
belongs_to :station
end
class Station < ActiveRecord::Base
has_many :orders
end