From: gga Date: 2007-01-19T23:00:07+09:00 Subject: Re: local variable assertion dflanagan@gmail.com ha escrito: > I've started studying Ruby, and while I like it, one thing that bothers > me is that there is not a way to explicitly declare a variable in order > to say "I want a variable local to this scope: I don't want to reuse > some variable by the same name in a containing scope." In particular, > the fact that giving a block parameter the same name as an existing > variable can overwrite that variable troubles me. > > Now I haven't written enough Ruby code to know whether this is really a > problem in practice or if it is just a theoretical concern. I think it is more theoretical, as that would indicate you are really writing VERY long functions. Remember that in Ruby, global variables are $, instance variables are @ and class variables are @@, so there's a very rare chance of conflict. That being said, your code can be done simpler, like: def let(*syms, &block) raise "No block provided for undefined?" unless block_given? syms.each do |sym| value = eval("defined? #{sym.to_s}", block) next if !value raise NameError.new("#{sym} is already a #{value}") end yield end # block check let( :x ) { x = 20 } begin let( :x ) { p 'never run' } rescue end # Forgot block... let( :x )