Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > setting local variables in a binding

Reply
Thread Tools

setting local variables in a binding

 
 
Martin DeMello
Guest
Posts: n/a
 
      10-06-2010
Why does this not work?

ruby-1.9.2-p0 > b = binding
=> #<Binding:0x9adc81c>
ruby-1.9.2-p0 > eval("x = 5", b)
=> 5
ruby-1.9.2-p0 > x
NameError: undefined local variable or method `x' for main:Object
from (irb):6
from /home/martin/.rvm/rubies/ruby-1.9.2-p0/bin/irb:17:in `<main>'
ruby-1.9.2-p0 > eval("x")
NameError: undefined local variable or method `x' for main:Object
from (irb):7:in `eval'
from (irb):7:in `eval'
from (irb):7
from /home/martin/.rvm/rubies/ruby-1.9.2-p0/bin/irb:17:in `<main>'
ruby-1.9.2-p0 > eval("x", b)
=> 5

martin

 
Reply With Quote
 
 
 
 
Brian Candler
Guest
Posts: n/a
 
      10-06-2010
Martin DeMello wrote:
> Why does this not work?
>
> ruby-1.9.2-p0 > b = binding
> => #<Binding:0x9adc81c>
> ruby-1.9.2-p0 > eval("x = 5", b)
> => 5
> ruby-1.9.2-p0 > x
> NameError: undefined local variable or method `x' for main:Object
> from (irb):6
> from /home/martin/.rvm/rubies/ruby-1.9.2-p0/bin/irb:17:in `<main>'


irb(main):001:0> b = binding
=> #<Binding:0x7f9079ec7460>
irb(main):002:0> eval("x = 5", b)
=> 5
irb(main):003:0> x
=> 5
irb(main):004:0> RUBY_DESCRIPTION
=> "ruby 1.8.7 (2010-01-10 patchlevel 249) [x86_64-linux]"

It might be an irb-ism in 1.9. Have you tried it in a standalone .rb
program?
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Martin DeMello
Guest
Posts: n/a
 
      10-06-2010
On Wed, Oct 6, 2010 at 6:13 PM, Brian Candler <(E-Mail Removed)> wrote:
>
> It might be an irb-ism in 1.9. Have you tried it in a standalone .rb
> program?


Yes, didn't work there either. Didn't think to try 1.8 - wonder why
the behaviour changed.

martin

 
Reply With Quote
 
John Sikora
Guest
Posts: n/a
 
      10-06-2010
Martin DeMello wrote:
> Why does this not work?
>
> ruby-1.9.2-p0 > b = binding
> => #<Binding:0x9adc81c>
> ruby-1.9.2-p0 > eval("x = 5", b)
> => 5
> ruby-1.9.2-p0 > x
> NameError: undefined local variable or method `x' for main:Object
> from (irb):6
> from /home/martin/.rvm/rubies/ruby-1.9.2-p0/bin/irb:17:in `<main>'
> ruby-1.9.2-p0 > eval("x")
> NameError: undefined local variable or method `x' for main:Object
> from (irb):7:in `eval'
> from (irb):7:in `eval'
> from (irb):7
> from /home/martin/.rvm/rubies/ruby-1.9.2-p0/bin/irb:17:in `<main>'
> ruby-1.9.2-p0 > eval("x", b)
> => 5
>
> martin


Hmmm, it seems to work when x is already defined (in 1.9.2):

irb(main):001:0> x=4
=> 4
irb(main):002:0> b=binding
=> #<Binding:0xfa2ab0>
irb(main):003:0> eval("x=5", b)
=> 5
irb(main):004:0> x
=> 5
irb(main):005:0>

If I do not set x to a value beforehand, I get the same error. Do not
know why though.

js
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Martin DeMello
Guest
Posts: n/a
 
      10-06-2010
On Thu, Oct 7, 2010 at 12:38 AM, John Sikora <(E-Mail Removed)> wrote:
> Hmmm, it seems to work when x is already defined (in 1.9.2):
>
> irb(main):001:0> x=4
> => 4
> irb(main):002:0> b=binding
> => #<Binding:0xfa2ab0>
> irb(main):003:0> eval("x=5", b)
> => 5
> irb(main):004:0> x
> => 5
> irb(main):005:0>
>
> If I do not set x to a value beforehand, I get the same error. Do not
> know why though.


Oddly enough, if x is defined beforehand you don't even need the binding:

ruby-1.9.2-p0 > x = 5
=> 5
ruby-1.9.2-p0 > eval "x = 42"
=> 42
ruby-1.9.2-p0 > x
=> 42

martin

 
Reply With Quote
 
Tony Arcieri
Guest
Posts: n/a
 
      10-06-2010
[Note: parts of this message were removed to make it a legal post.]

Note that "binding" is the default argument of eval if you don't specify
one, so capturing binding as a variable and passing it explicitly is
redundant.

On Wed, Oct 6, 2010 at 1:53 PM, Martin DeMello <(E-Mail Removed)>wrote:

> On Thu, Oct 7, 2010 at 12:38 AM, John Sikora <(E-Mail Removed)>
> wrote:
> > Hmmm, it seems to work when x is already defined (in 1.9.2):
> >
> > irb(main):001:0> x=4
> > => 4
> > irb(main):002:0> b=binding
> > => #<Binding:0xfa2ab0>
> > irb(main):003:0> eval("x=5", b)
> > => 5
> > irb(main):004:0> x
> > => 5
> > irb(main):005:0>
> >
> > If I do not set x to a value beforehand, I get the same error. Do not
> > know why though.

>
> Oddly enough, if x is defined beforehand you don't even need the binding:
>
> ruby-1.9.2-p0 > x = 5
> => 5
> ruby-1.9.2-p0 > eval "x = 42"
> => 42
> ruby-1.9.2-p0 > x
> => 42
>
> martin
>
>



--
Tony Arcieri
Medioh! A Kudelski Brand

 
Reply With Quote
 
John Sikora
Guest
Posts: n/a
 
      10-07-2010
Tony Arcieri wrote:
> Note that "binding" is the default argument of eval if you don't specify
> one, so capturing binding as a variable and passing it explicitly is
> redundant.


You are correct but the problem 'persists'. The following irb session in
1.9.2 shows the same thing without the binding:

irb(main):001:0> eval("x=5")
=> 5
irb(main):002:0> x
NameError: undefined local variable or method `x' for main:Object
from (irb):2
from C:/Ruby192/bin/irb:12:in `<main>'
irb(main):003:0> x=4
=> 4
irb(main):004:0> eval("x=5")
=> 5
irb(main):005:0> x
=> 5
irb(main):006:0>


In order for this to work, the x needs to be @x per the following irb
session (again 1.9.2):

irb(main):001:0> eval("@x=5")
=> 5
irb(main):002:0> @x
=> 5
irb(main):003:0>

The behavior seen in the first irb session was the same in 1.8.6, 1.9.1,
and 1.9.2 when run standalone. However, it works in irb for 1.8.6 (x w/o
the @).

I have sometimes seen 'local' or 'unassociated' instance variables (not
sure if my terminology is correct) in example programs and I have
wondered why they are used. I guess this is one reason although most
people don't like to use eval.

I use eval in personal scripts since it is so convenient. Had to go back
and look at some code to remind myself how I got around the behavior
seen in the first irb session. I don't remember if I read about using
the local instance variable or just played around with it until I got it
to work.

js
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      10-07-2010
On Thu, Oct 7, 2010 at 4:19 AM, John Sikora <(E-Mail Removed)> wrote:
> Tony Arcieri wrote:
>> Note that "binding" is the default argument of eval if you don't specify
>> one, so capturing binding as a variable and passing it explicitly is
>> redundant.

>
> You are correct but the problem 'persists'. The following irb session in
> 1.9.2 shows the same thing without the binding:
>
> irb(main):001:0> eval("x=3D5")
> =3D> 5
> irb(main):002:0> x
> NameError: undefined local variable or method `x' for main:Object
> =A0 =A0 =A0 =A0from (irb):2
> =A0 =A0 =A0 =A0from C:/Ruby192/bin/irb:12:in `<main>'
> irb(main):003:0> x=3D4
> =3D> 4
> irb(main):004:0> eval("x=3D5")
> =3D> 5
> irb(main):005:0> x
> =3D> 5
> irb(main):006:0>


The reason is that Ruby looks at the source code to find out which are
local variables. This is basically the same as doing

13:00:57 ~$ ruby19 -e 'def f;puts "x=3D10";p x end;f'
x=3D10
-e:1:in `f': undefined local variable or method `x' for main:Object (NameEr=
ror)
from -e:1:in `<main>'
13:00:59 ~$ ruby19 -e 'def f;eval "x=3D10";p x end;f'
-e:1:in `f': undefined local variable or method `x' for main:Object (NameEr=
ror)
from -e:1:in `<main>'
13:01:02 ~$

In other words: the interpreter does not know that there is an
assignment to x in the eval code so x is undefined.

> In order for this to work, the x needs to be @x per the following irb
> session (again 1.9.2):
>
> irb(main):001:0> eval("@x=3D5")
> =3D> 5
> irb(main):002:0> @x
> =3D> 5
> irb(main):003:0>


That's an instance variable which is a completely different beast.

> The behavior seen in the first irb session was the same in 1.8.6, 1.9.1,
> and 1.9.2 when run standalone. However, it works in irb for 1.8.6 (x w/o
> the @).


irb has some specialities with handling local variables which is the
reason it behaves differently. The reason is that it will not parse
the code in one go because it only ever sees a chunk (every time you
hit enter). If the interpreter's regular behavior would be retained
you could not use local variables in irb which would be inconvenient.

> I have sometimes seen 'local' or 'unassociated' instance variables (not
> sure if my terminology is correct)


Not sure what you actually mean by that.

> in example programs and I have
> wondered why they are used. I guess this is one reason although most
> people don't like to use eval.


The reasons against eval are more in the area of safety and
performance. If you know which variable you want to assign you can
use it directly. If you do not know you typically use a Hash.

> I use eval in personal scripts since it is so convenient. Had to go back
> and look at some code to remind myself how I got around the behavior
> seen in the first irb session. I don't remember if I read about using
> the local instance variable or just played around with it until I got it
> to work.


What do you use eval for? In cases where the set of variables is not
fixed at coding time chances are that you usually want a Hash. Using
eval should be really be rare and for specific cases but not an
everyday habit IMHO.

Kind regards

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

 
Reply With Quote
 
John Sikora
Guest
Posts: n/a
 
      10-08-2010
Robert Klemme wrote:
> On Thu, Oct 7, 2010 at 4:19 AM, John Sikora <(E-Mail Removed)>
> wrote:
>> I have sometimes seen 'local' or 'unassociated' instance variables (not
>> sure if my terminology is correct)

>
> Not sure what you actually mean by that.


I mean that I have seen examples where people use, say '@var', outside
of any class, module, or method; in the 'main body' of the program. I
would think that they would just use 'var' instead. I do not see why it
would be advantageous to use '@var'. Is there a reason to do this?

>> I use eval in personal scripts since it is so convenient. Had to go back
>> and look at some code to remind myself how I got around the behavior
>> seen in the first irb session. I don't remember if I read about using
>> the local instance variable or just played around with it until I got it
>> to work.

>
> What do you use eval for? In cases where the set of variables is not
> fixed at coding time chances are that you usually want a Hash. Using
> eval should be really be rare and for specific cases but not an
> everyday habit IMHO.
>


I mainly use 'eval' to evaluate syntactic sugar expressions for
enumerable methods such as 'sort', 'find_all', 'all?', etc. I have
created a Module with enumerable classes (they have an 'each' method)
and I have added syntactic sugar to some of the methods. So I can be
lazy (and quick) and write things like:

Slot.enum.find_all(‘:slot_num >= 8’, ‘:slot_num <= 22’, “:rate ==
‘ufec’”).all?(‘:errors == 0’)

As you can probably guess, anything with a ':' before it is an attribute
that gets evaluated. Then the expression gets evaluated. I know I could
use ‘send’ for the first evaluation, but sometimes I chain together
attributes of attributes, use variables in the expressions, etc., and it
gets more complicated. 'eval' is my easy way out (I’m just a hardware
engineer trying to make my life easier). In case anyone is wondering,
the parameters given in 'find_all' have the option of an ‘and’ or ‘or’
function. Default is ‘and’ as in the example.

Speed is generally not an issue and I am somewhat aware of the dangers
of using 'eval'. I try to isolate the 'eval' expressions by limiting the
user (me, mostly) to commands that have pre-programmmed options. A
malicious user could probably find a way around it though since I am not
too savvy in this area.

I am always looking to improve the code and if anyone can suggest ways
of eliminating the 'eval' statements while keeping the same
functionality, I will certainly listen. I am also trying to read up on
the latest dynamic techniques that rely less upon 'eval'.

js
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      10-08-2010
On Fri, Oct 8, 2010 at 6:06 AM, John Sikora <(E-Mail Removed)> wrote:
> Robert Klemme wrote:
>> On Thu, Oct 7, 2010 at 4:19 AM, John Sikora <(E-Mail Removed)>
>> wrote:
>>> I have sometimes seen 'local' or 'unassociated' instance variables (not
>>> sure if my terminology is correct)

>>
>> Not sure what you actually mean by that.

>
> I mean that I have seen examples where people use, say '@var', outside
> of any class, module, or method; in the 'main body' of the program. I
> would think that they would just use 'var' instead. I do not see why it
> would be advantageous to use '@var'. Is there a reason to do this?
>
>>> I use eval in personal scripts since it is so convenient. Had to go bac=

k
>>> and look at some code to remind myself how I got around the behavior
>>> seen in the first irb session. I don't remember if I read about using
>>> the local instance variable or just played around with it until I got i=

t
>>> to work.

>>
>> What do you use eval for? =A0In cases where the set of variables is not
>> fixed at coding time chances are that you usually want a Hash. =A0Using
>> eval should be really be rare and for specific cases but not an
>> everyday habit IMHO.

>
> I mainly use 'eval' to evaluate syntactic sugar expressions for
> enumerable methods such as 'sort', 'find_all', 'all?', etc. I have
> created a Module with enumerable classes (they have an 'each' method)


So you have something like

class Foo
def self.each
yield "whatever"
self
end
end

? Did you also do

class Foo
def self.each
yield "whatever"
end

extend Enumerable
end

? That would give you all methods like #find_all etc. for free.

> and I have added syntactic sugar to some of the methods. So I can be
> lazy (and quick) and write things like:
>
> Slot.enum.find_all(=91:slot_num >=3D 8=92, =91:slot_num <=3D 22=92, =93:r=

ate =3D=3D
> =91ufec=92=94).all?(=91:errors =3D=3D 0=92)


Is this equivalent to

enum.find_all {|s| (8..22) =3D=3D=3D s.slot_num && s.rate =3D=3D 'ufec'}.al=
l?
{|s| e.errors =3D=3D 0}

? If yes, I do not really see the advantage of your approach. It's
slower needs a similar amount of typing and will detect errors later
(at execution time vs. at parse time).

> As you can probably guess, anything with a ':' before it is an attribute
> that gets evaluated. Then the expression gets evaluated. I know I could
> use =91send=92 for the first evaluation, but sometimes I chain together
> attributes of attributes, use variables in the expressions, etc., and it
> gets more complicated. 'eval' is my easy way out (I=92m just a hardware
> engineer trying to make my life easier). In case anyone is wondering,
> the parameters given in 'find_all' have the option of an =91and=92 or =91=

or=92
> function. Default is =91and=92 as in the example.


If my assumption above is correct I do not understand where the "easy
way out" is.

> Speed is generally not an issue and I am somewhat aware of the dangers
> of using 'eval'. I try to isolate the 'eval' expressions by limiting the
> user (me, mostly) to commands that have pre-programmmed options. A
> malicious user could probably find a way around it though since I am not
> too savvy in this area.
>
> I am always looking to improve the code and if anyone can suggest ways
> of eliminating the 'eval' statements while keeping the same
> functionality, I will certainly listen. I am also trying to read up on
> the latest dynamic techniques that rely less upon 'eval'.


(see above)

Kind regards

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Setting local variables for 'erb' command-line tool Brian Candler Ruby 2 10-14-2010 04:26 PM
setting variables in the local namespace Chris Withers Python 8 10-13-2009 08:47 PM
setting local variables via eval ara howard Ruby 15 03-12-2008 12:02 PM
Can local function access local variables in main program? Sullivan WxPyQtKinter Python 10 11-08-2007 02:51 PM
Binding local variables to a signal connection Matthias Kaeppler C++ 3 12-01-2005 01:45 PM



Advertisments