Skip to content

Instantly share code, notes, and snippets.

@mhewedy
Created October 2, 2013 12:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mhewedy/6793089 to your computer and use it in GitHub Desktop.
Save mhewedy/6793089 to your computer and use it in GitHub Desktop.
Orders Aggregator
# Orders Aggregator
class BreadType
attr_accessor :type
def initialize(type)
@type = type
end
def eql?(other)
@type = other.type
end
def hash
@type.hash
end
def to_s
@type
end
end
class Sandwich
attr_accessor :name, :type
def initialize (name, type)
@name = name
@type = type
end
def eql?(other)
@name = other.name #&& @type = other.type # buggy and I cannot resolve, so I decided to comment
end
def hash
@name.hash ^ @type.hash
end
def to_s
@name.to_s << " : " << @type.to_s
end
end
balady = BreadType.new "Balady"
shamy = BreadType.new "Shamy"
# Talaat
talaat = []
talaat << Sandwich.new(:Ta3mya, balady) << Sandwich.new(:Batates_omlete, balady)
# Hewedy
hewedy = []
2.times {
hewedy << Sandwich.new(:Ta3mya, balady)
}
# Ahmad
ahmad = []
ahmad << Sandwich.new(:Swabe3_Salada, balady) << Sandwich.new(:Fol_Eskandrany, balady)
# Khalid
khalid = []
khalid << Sandwich.new(:Ta3mya_Eskandrany, balady)
# Alaa
alaa = []
alaa << Sandwich.new(:Ta3mya_Eskandrany, balady)
# Hatem
hatem = []
3.times {
hatem << Sandwich.new(:Ta3mya, shamy)
}
total = talaat + hewedy + ahmad + khalid + alaa + hatem
counts = Hash.new(0)
total.each { |name| counts[name] += 1 }
puts "Aggregation: "
counts.each { |k, v|
puts k.to_s << " ::" << v.to_s
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment