From: Vasco Andrade e Silva Date: 2007-11-16T02:38:46+09:00 Subject: Re: Shadowing bug?? Robert Klemme wrote: > 2007/11/14, Vasco Andrade e Silva : >> > >> 1 >> >> > f_ko({:xpto => "ohoh"}) >> :in_lambda >> "ohoh" >> {:xpto=>"ohoh"} >> "ohoh" >> {:xpto=>"ohoh"} # :-) > > Just let me add that even though this is fixed IMHO it is a bad idea > to use the same identifier in both cases if you want to keep them > separate. This can likely cause confusion. I don't agreed with you. Shadowing, IMHO, is a very useful (and powerful) way to express semantic and execute "best practices". Example: Imagine you want to process a tree (as i wanted). Suppose that in the process of that computation you call "defn_to_sexp". This method receives a node (tagged with :defn). In this case i wanted to do a "special" process to the args branch of the tree. So, and because i didn't want to declare another function visible from class scope, i created a proc (process_args). This proc should receive the tree branch (i.e. "node") and other variable witch represents a computation over some other branch of defn's node. Here goes a simple sample: def defn_to_sexp(node) process_args = lambda do |n, has_block| ... end ... has_block = process(...) process_args.call(args_node(node), hash_block) ... end i have more semantic if i can call the word "node" instead of the word "n" in lambda's arg: I want to shadow the node variable. This is a case, IMHO, where it's a good idea "to use the same identifier in both cases". In this case i would wanted to write: def defn_to_sexp(node) process_args = lambda do |node, has_block| ... # i don't want to access to the outer node # and "node" says more than "n" end ... has_block = process(...) process_args.call(args_node(node), hash_block) ... end Regards, Vasco Andrade e Silva -- Posted via http://www.ruby-forum.com/.