#!/usr/bin/env ruby # This code comes from http://ruby-it.org/pages/Creare+Enumerable # Check the page for copyright notice and explanations enumerabile.each do |x| fai cose con x end >> class Tripla >> def initialize(a,b,c) >> @a,@b,@c=a,b,c >> end >> def each >> yield @a >> yield @b >> yield @c >> end >> end => nil >> t=Tripla.new 10, 20, 30 => # >> t.each { |x| puts x } 10 20 30 => nil >> class Tripla >> include Enumerable >> end => Tripla >> t=Tripla.new 17,42,11 => # >> puts t.sort # ordiniamoli 11 17 42 => nil >> puts t.map {|x| x*2} # raddoppiamo i valori 34 84 22 => nil >> puts t.select {|x| x%2==1} # selezioniamo i dispari 17 11 => nil