2010/4/23 Derek Cannon <novellterminator / gmail.com>:
> If you guys need some better clarification as to what these methods do:
>
> Parameter explanation:
> Each hash passed into overlaps?(x,y) will have a key (day of the week)
> linked to an array of ranges (times through the day).
> E.g.
>   ὾ ۱ ݬ   ۱ >   ὾ ۱ݺ  ۴ >
> Methods explanation:
> These methods are used to make sure that no two hashes have conflicting
> times (ranges in the array for each key) on the same day (same key).
>
> In the above example, the overlaps? method would see that both x and y
> hashes have the same keys (meaning they're on the same days), so it
> would continue to investigate the values further. (If they didn't share
> at least one common day, their times could never conflict because they'd
> be on different days.)
>
> In the next step, the code compares every range in the array (of every
> key) of the two hashes, and ensures that no two times overlap on the
> same day.
>
> Does this make it any clearer? Or is no one answering this because I've
> made it as simple/Ruby-esque as it can get?

I would start out by creating at least two or three classes.  One for
what is a Hash in your case (maybe call it TimeTable or such), one for
a list of ranges and maybe one for a TimeRange (which could make sure
parameter values are legal, i.e. in the range 0..23 etc.).

Then I would place those methods in classes appropriate for it and the
code will become much more readable and maintainable.

Regarding the algorithm, I believe you are not checking what you
described in this piece of code:

   a.each_key { |i|
     b.each_key { |j|
       # Check each element of each array to see if any ranges overlap
       if range_overlaps?(a[i], b[j])
         return true
       end
     }
   }

If I'm not mistaken that will check all days vs. each other.  You
probably rather want:

require 'set'
...
(a.keys.to_set + b.keys).any? do |day|
  r1 = a[day]
  r2 = b[day]
  r1 && r2 && range_overlaps?(r1, r2)
end

(With my suggested change that line would rather look like

r1 && r2 && r1.overlaps? r2

since then you had a class where to stuff the overlap check into.)

Kind regards

robert

-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/