0

currently I'm about to learn this awesome language and want to build a small calculator for the online game "Eve Online".

I'm struggling with this part of code

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}

Obviously the "p orders.select" doesn't work.

What I want to achieve is to retrieve, say 10 cheapest prices, for a certain typeid.

I do like the approach given here: How do I search within an array of hashes by hash values in ruby?

However this forces me to keep hashes in an array and then again, I can't nest them.

What I don't want to do, is to nest 3 ".each do |key, value|", because (I suppose at least) it would lead to complexity of O(n^3), which should be quite bad.

So is there a way to retrieve all :price - values for a certain type in a smart way?

Thanks to everybody in advance!

Community
  • 1
  • 1
mohnstrudel
  • 639
  • 8
  • 22
  • It would probably be easier to use an *array* of orders, instead of a nested hash structure, i.e. `orders = [{station: '...', type: '...', price: '...'}, {stat...}]` – Stefan Feb 27 '15 at 12:06
  • that works! wouldn't it be, however, an issue when amount of orders will increase? I thought that is the benefit of using hashes and symbols - they're incredibly fast. – mohnstrudel Feb 27 '15 at 12:30
  • Lookup is fast when using a key, searching through a hash is just as slow. – Stefan Feb 27 '15 at 13:01
  • There appears to be some kind of mistake in the sample data: The hash starting with `typeid: "type2"` is not in `orders` as you probably intend for it to be. – Wayne Conrad Feb 27 '15 at 13:25
  • Thank's for pointing out! Strangely enough it worked, but I guess it's because I only wanted to retrieve the "type1" sub-hash. – mohnstrudel Feb 27 '15 at 15:13

1 Answers1

2

I would use an array:

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"}
#   ]

Since you are using Ruby on Rails, you should use models and associations, something like:

class Order < ActiveRecord::Base
  belongs_to :station
end

class Station < ActiveRecord::Base
  has_many :orders
end
Stefan
  • 109,145
  • 14
  • 143
  • 218
  • Thank you very much! Although I'm not yet using Rails, just pure Ruby. I want to create a fully functional - can you call it this way? - backend for my calculator, then move it online. And sorry, can't upvote your answer as it requires 15 reputation first, however I think I marked it as the right answer. – mohnstrudel Feb 27 '15 at 14:09
  • Your question has a [tag:ruby-on-rails] tag :-) – Stefan Feb 27 '15 at 14:12