nicknovitski/tabletop

View on GitHub
lib/tabletop/table.rb

Summary

Maintainability
A
0 mins
Test Coverage
module Tabletop
  class Table
    def initialize(collection)
      @collection = collection
    end
    
    def [](index)
      index = index-1 if @collection.is_a? Array
      raise KeyError if index < 0
      @collection.fetch(index) do
        enum_and_value = @collection.detect { |k,v| Array(k).include?(index) }
        raise KeyError unless enum_and_value
        enum_and_value[1]
      end
    end
  end
end