From: Mark Hubbart Date: 2004-12-04T11:02:52+09:00 Subject: Re: any way to manipulate variables and bindings? On Fri, 3 Dec 2004 21:42:47 +0900, Robert Klemme wrote: > > "itsme213" schrieb im Newsbeitrag > news:I7Jrd.100267$jq5.22882@fe2.texas.rr.com... > > > > def foo > > bar [:a, :b, :c] > > a > > b > > c > > end > > > > I would like a, b, and c to be local variables with values 1, 2, 3 > > (positions of :a, :b, :c). Is there some implementation of > > def bar (symbol_list) > > symbol_list.each_with_index { |s, i| > > ??? > > } > > end > > that will do this? > > As Mark pointed out, you can't get this to work because of the compile > time decisions Ruby takes. I should clarify: Ruby will set the variables, they are just inaccessible unless you assign to them at some point in that binding's context: mark@eMac% ruby eval "a = 23" p a ^D -:2: undefined local variable or method `a' for main:Object (NameError) mark@eMac% ruby eval "a = 23" a += 19 p a ^D 42 So, as long as you assign to them anywhere in that context, it's okay. even if it's after they have been set once. head.spin(:rapidly) I think the reason for this is that there was a tradeoff made: to do what you want to do would require that variables be checked at runtime, not eval-time. This would prevent early detection of errors related to variable names. I guess it was decided that the error detection was more important than a slightly more flexible/dynamic local variable setup. cheers, Mark