#!/usr/bin/env ruby # This code comes from http://ruby-it.org/pages/Multiplexer # Check the page for copyright notice and explanations class Multiplexer def initialize(*oggetti) @ogg=oggetti end def method_missing(metodo,*args,&blk) @ogg.each { |o| o.send(metodo,*args,&blk) } end end >> require 'multiplexer' => true >> require 'stringio' => true >> f1,f2 = StringIO.new, StringIO.new => [#, #] >> m=Multiplexer.new(f1,f2) => #, #]> >> m.puts 'ciao!' => [#, #] >> f1.string => "ciao!\n" >> f2.string => "ciao!\n" => nil >> str,ary= 'ciao', [1] => ["ciao", [1]] >> m=Multiplexer.new(str,ary) => # >> m << 'accodiamo' # tutti e due gli oggetti rispondono a '<<' => ["ciaoaccodiamo", [1, "accodiamo"]] >> str => "ciaoaccodiamo" >> ary => [1, "accodiamo"] >> m.upcase! # un array non sa come diventare maiuscolo! NoMethodError: undefined method `upcase' for [1, "accodiamo"]:Array from ./multiplexer.rb:6:in `send' from ./multiplexer.rb:6:in `method_missing' from ./multiplexer.rb:6:in `each' from ./multiplexer.rb:6:in `method_missing' from (irb):16 print x if x == 'ciao' def method_missing(met,*arg,&blk) @ogg.each do |o| o.send(met,*arg,&blk) rescue nil end end >> m=Multiplexer.new('ciao',[]) => # >> m.capitalize! => ["Ciao", []]