Advertisement
Guest User

s

a guest
Jan 20th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.95 KB | None | 0 0
  1. class Teil
  2.  
  3.   include Enumerable
  4.  
  5.   attr_reader :name, :length, :color
  6.  
  7.   def initialize(name, length, color)
  8.     @name = name
  9.     @length = length
  10.     @color = color
  11.  
  12.     @teile = []
  13.   end
  14.  
  15.   def add(teil)
  16.     raise ArgumentError unless teil.is_a? Teil
  17.     @teile.push(teil)
  18.   end
  19.  
  20.   def remove(teil)
  21.     raise ArgumentError unless teil.is_a? Teil
  22.     raise ArgumentError unless @teile.include?(teil)
  23.  
  24.     @teile.delete(teil)
  25.   end
  26.  
  27.   def each(&block)
  28.     if block_given?
  29.       @teile.each(&block)
  30.     else
  31.       @teile.each
  32.     end
  33.   end
  34.  
  35.   def include?(teil)
  36.     @teile.include?(teil)
  37.   end
  38.  
  39.   def ==(other)
  40.     [@name, @length, @color].eql?([other.name, other.length, other.color])
  41.   end
  42.  
  43.   def count
  44.     c = @teile.count + 1
  45.     @teile.each do |t|
  46.       c += t.each.size
  47.     end
  48.  
  49.     c
  50.   end
  51.  
  52.   def hash
  53.     @name.hash + @length.hash + @color.hash
  54.   end
  55.  
  56.   def to_s
  57.     @teile.to_s
  58.   end
  59.  
  60.   alias eql? ==
  61. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement