#!/usr/bin/env ruby # This code comes from http://ruby-it.org/pages/Funzionale+Map # Check the page for copyright notice and explanations ary=Array.new for i in oggEnumerable ary.push(funzione(i)) end puts ary puts oggEnumerable.map {|i| funzione(i)} puts oggEnumerable.map &funzione print File.new('esempio.txt').map {|linea| linea.reverse} puts File.new('esempio.txt').map {|linea| linea.length} >> class Tripla >> def initialize(a,b,c) >> @a,@b,@c=a,b,c >> end >> def each >> yield @a >> yield @b >> yield @c >> end >> include Enumerable # ecco la magia! >> end => Tripla >> t=Tripla.new( 13,'miao',[1,2,3] ) => # >> t.map {|x| x * 2 } => [26, "miaomiao", [1, 2, 3, 1, 2, 3]]