Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Teil
- include Enumerable
- attr_reader :name, :length, :color
- def initialize(name, length, color)
- @name = name
- @length = length
- @color = color
- @teile = []
- end
- def add(teil)
- raise ArgumentError unless teil.is_a? Teil
- @teile.push(teil)
- end
- def remove(teil)
- raise ArgumentError unless teil.is_a? Teil
- raise ArgumentError unless @teile.include?(teil)
- @teile.delete(teil)
- end
- def each(&block)
- if block_given?
- @teile.each(&block)
- else
- @teile.each
- end
- end
- def include?(teil)
- @teile.include?(teil)
- end
- def ==(other)
- [@name, @length, @color].eql?([other.name, other.length, other.color])
- end
- def count
- c = @teile.count + 1
- @teile.each do |t|
- c += t.each.size
- end
- c
- end
- def hash
- @name.hash + @length.hash + @color.hash
- end
- def to_s
- @teile.to_s
- end
- alias eql? ==
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement