#!/usr/bin/env ruby # This code comes from http://ruby-it.org/pages/Hash+javascript-like # Check the page for copyright notice and explanations var mioHash = {}; mioHash["kiave"] = "valore"; mioHash.kiave = "valore"; >> class Mia >> def method_missing(sym,*args,&blk) >> puts 'chiamato: '+sym.to_s >> puts 'con argomenti: '+args.inspect >> puts 'ed un blocco' if blk >> end >> end => nil >> m=Mia.new => # >> m.ciao chiamato: ciao con argomenti: [] => nil >> m.ciao 1 ,2 ,3 chiamato: ciao con argomenti: [1, 2, 3] => nil >> m.ciao(1 ,2 ,3) { lol } chiamato: ciao con argomenti: [1, 2, 3] ed un blocco => nil >> class Hash >> def method_missing(sym,*args,&blk) >> self[sym.to_s] >> end >> end => nil >> hsh={} => {} >> hsh['ciao']='bau' => "bau" >> hsh.ciao => "bau"