Mit der Methode 'yield' ruft man einen übergebenen Code-Block auf.
Das Enumerable Mixin bietet eine Reihe nützlicher Methoden, die auf 'each' basieren.
class Sammlung3
include Enumerable
def initialize(a, b, c)
@a, @b, @c = a, b, c
end
def each
yield @a
yield @b
yield @c
end
end
sammlung = Sammlung3.new(4, 2, 6)
sammlung.each { |x| puts "Wert: #{x}" }
p sammlung.sort
puts "max = #{sammlung.max}, min = #{sammlung.min}"
p sammlung.collect { |x| x * x }
Wert: 4 Wert: 2 Wert: 6 [2, 4, 6] max = 6, min = 2 [16, 4, 36]