[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Dynamic local vars

Vasyl Smirnov

12/7/2007 8:20:00 AM

Hi,

I wonder if there is any way to create local variables dynamically,
for example, given

def foo
bar
puts x, y
end

is it possible for bar to somehow create and initialize x and y?
Binding doesn't seem to be modifiable..

Or am I asking too much? :)

13 Answers

MonkeeSage

12/7/2007 9:32:00 AM

0

On Dec 7, 2:19 am, Vasyl Smirnov <vasyl.smir...@gmail.com> wrote:
> Hi,
>
> I wonder if there is any way to create local variables dynamically,
> for example, given
>
> def foo
> bar
> puts x, y
> end
>
> is it possible for bar to somehow create and initialize x and y?
> Binding doesn't seem to be modifiable..
>
> Or am I asking too much? :)

There are probably more clever ways than this, but I'm dumb...

def bar
x = 10
y = 20
binding
end

def foo
eval("puts x, y", bar)
end

Regards,
Jordan

Tim Hunter

12/7/2007 1:06:00 PM

0

Vasyl Smirnov wrote:
> Hi,
>
> I wonder if there is any way to create local variables dynamically,
> for example, given
>
> def foo
> bar
> puts x, y
> end
>
> is it possible for bar to somehow create and initialize x and y?
> Binding doesn't seem to be modifiable..
>
> Or am I asking too much? :)
>

Hmmm...local variables are called local because they're local to the
method that uses them. A variable that's shared between two methods
isn't local.

If foo and bar are both methods in the same class, use instance
variables. If they're not both in the same class, use global variables.

--
RMagick: http://rmagick.ruby...

Vasyl Smirnov

12/7/2007 1:26:00 PM

0



On Dec 7, 11:35 am, MonkeeSage <MonkeeS...@gmail.com> wrote:
> On Dec 7, 2:19 am, Vasyl Smirnov <vasyl.smir...@gmail.com> wrote:
>
> def bar
> x = 10
> y = 20
> binding
> end
>
> def foo
> eval("puts x, y", bar)
> end
>

This would do, but I'd like to have the content of foo as simple as
possible (that's actually the reason for the question).


Robert Klemme

12/7/2007 1:28:00 PM

0

2007/12/7, Vasyl Smirnov <vasyl.smirnov@gmail.com>:
> I wonder if there is any way to create local variables dynamically,
> for example, given
>
> def foo
> bar
> puts x, y
> end
>
> is it possible for bar to somehow create and initialize x and y?
> Binding doesn't seem to be modifiable..

You cannot do this easily because of the method / variable ambiguity.
There's a hack to do it: you need to define them before you use them
but it is ugly and does not work properly.

14:23:17 ~
$ ruby <<XXX
> def foo
> x=y=nil
> bar(binding)
> puts x,y
> end
> def bar(b)
> eval("x=1;y=2",b)
> end
> foo
> XXX
1
2

The problem with dynamically introducing local variables is that your
code needs to be statically aware of them in order to use them. Even
though you can inject any number of additional local variables into a
binding, they won't get used because they do not appear in the code of
that method.

A much better solution to the problem of storing dynamic values is a Hash.

def foo
data = {}
bar data
puts data[:x], data[:y]
end

def bar(x)
x[:x] = 1
x[:y] = 2
end

But you can as well return multiple values

def foo
x,y = bar
puts x, y
end

def bar
return 1,2
end

Kind regards

robert

--
use.inject do |as, often| as.you_can - without end

Vasyl Smirnov

12/7/2007 1:30:00 PM

0



On Dec 7, 3:06 pm, Tim Hunter <TimHun...@nc.rr.com> wrote:
> Vasyl Smirnov wrote:
> > Hi,
>
> > I wonder if there is any way to create local variables dynamically,
> > for example, given
>
> > def foo
> > bar
> > puts x, y
> > end
>
> > is it possible for bar to somehow create and initialize x and y?
> > Binding doesn't seem to be modifiable..
>
> > Or am I asking too much? :)
>
> Hmmm...local variables are called local because they're local to the
> method that uses them. A variable that's shared between two methods
> isn't local.
>
> If foo and bar are both methods in the same class, use instance
> variables. If they're not both in the same class, use global variables.
>

Yes, instance vars are an option. Though what would be better is
something like a C-style macro,
which would just define and assign these local vars inside foo. I
thought maybe there's a way to do it in Ruby.

MonkeeSage and Tim, thank you both for your replies!

Vasyl Smirnov

12/7/2007 1:47:00 PM

0



On Dec 7, 3:28 pm, "Robert Klemme" <shortcut...@googlemail.com> wrote:
> 2007/12/7, Vasyl Smirnov <vasyl.smir...@gmail.com>:
>
> > I wonder if there is any way to create local variables dynamically,
> > for example, given
>
> > def foo
> > bar
> > puts x, y
> > end
>
> > is it possible for bar to somehow create and initialize x and y?
> > Binding doesn't seem to be modifiable..
>
> You cannot do this easily because of the method / variable ambiguity.
> There's a hack to do it: you need to define them before you use them
> but it is ugly and does not work properly.
>
> 14:23:17 ~
> $ ruby <<XXX> def foo
> > x=y=nil
> > bar(binding)
> > puts x,y
> > end
> > def bar(b)
> > eval("x=1;y=2",b)
> > end
> > foo
> > XXX
>
> 1
> 2
>
> The problem with dynamically introducing local variables is that your
> code needs to be statically aware of them in order to use them. Even
> though you can inject any number of additional local variables into a
> binding, they won't get used because they do not appear in the code of
> that method.
>

Thanks for clarifying that.


> A much better solution to the problem of storing dynamic values is a Hash.
>
> def foo
> data = {}
> bar data
> puts data[:x], data[:y]
> end
>
> def bar(x)
> x[:x] = 1
> x[:y] = 2
> end
>
> But you can as well return multiple values
>
> def foo
> x,y = bar
> puts x, y
> end
>
> def bar
> return 1,2
> end

The hash approach is nice, and I actually use it for some other code.

I've posted this question because I wanted to eliminate the need to
write "x, y = bar" and just go with "bar".
The reason is that I have a bunch of methods, each starting with a
kind of "x, y = bar", only with longer variable names.
So it looked like it would be nice to somehow reduce them.

So, it looks like I should either a) stop whining about it, or b) use
one of the proposed approaches.

MonkeeSage

12/7/2007 2:12:00 PM

0

On Dec 7, 7:47 am, Vasyl Smirnov <vasyl.smir...@gmail.com> wrote:
> On Dec 7, 3:28 pm, "Robert Klemme" <shortcut...@googlemail.com> wrote:
>
>
>
> > 2007/12/7, Vasyl Smirnov <vasyl.smir...@gmail.com>:
>
> > > I wonder if there is any way to create local variables dynamically,
> > > for example, given
>
> > > def foo
> > > bar
> > > puts x, y
> > > end
>
> > > is it possible for bar to somehow create and initialize x and y?
> > > Binding doesn't seem to be modifiable..
>
> > You cannot do this easily because of the method / variable ambiguity.
> > There's a hack to do it: you need to define them before you use them
> > but it is ugly and does not work properly.
>
> > 14:23:17 ~
> > $ ruby <<XXX> def foo
> > > x=y=nil
> > > bar(binding)
> > > puts x,y
> > > end
> > > def bar(b)
> > > eval("x=1;y=2",b)
> > > end
> > > foo
> > > XXX
>
> > 1
> > 2
>
> > The problem with dynamically introducing local variables is that your
> > code needs to be statically aware of them in order to use them. Even
> > though you can inject any number of additional local variables into a
> > binding, they won't get used because they do not appear in the code of
> > that method.
>
> Thanks for clarifying that.
>
>
>
> > A much better solution to the problem of storing dynamic values is a Hash.
>
> > def foo
> > data = {}
> > bar data
> > puts data[:x], data[:y]
> > end
>
> > def bar(x)
> > x[:x] = 1
> > x[:y] = 2
> > end
>
> > But you can as well return multiple values
>
> > def foo
> > x,y = bar
> > puts x, y
> > end
>
> > def bar
> > return 1,2
> > end
>
> The hash approach is nice, and I actually use it for some other code.
>
> I've posted this question because I wanted to eliminate the need to
> write "x, y = bar" and just go with "bar".
> The reason is that I have a bunch of methods, each starting with a
> kind of "x, y = bar", only with longer variable names.
> So it looked like it would be nice to somehow reduce them.
>
> So, it looks like I should either a) stop whining about it, or b) use
> one of the proposed approaches.

Not so much like a C macro, but more like a C struct...you could...use
a Struct ;)

V = Struct.new(:x, :y).new(nil, nil)

def bar
V.x = 10
V.y = 20
end
def foo
bar
puts V.x, V.y
end

Regards,
Jordan

Tim Hunter

12/7/2007 2:34:00 PM

0

Jordan Callicoat wrote:
>
> Not so much like a C macro, but more like a C struct...you could...use
> a Struct ;)
>
> V = Struct.new(:x, :y).new(nil, nil)
>
> def bar
> V.x = 10
> V.y = 20
> end
> def foo
> bar
> puts V.x, V.y
> end
>
> Regards,
> Jordan

Of course the Struct is not actually allowing him to share data between
bar and foo. It's the global constant V that's doing that.
--
Posted via http://www.ruby-....

MonkeeSage

12/7/2007 3:49:00 PM

0

On Dec 7, 8:33 am, Tim Hunter <rmag...@gmail.com> wrote:
> Jordan Callicoat wrote:
>
> > Not so much like a C macro, but more like a C struct...you could...use
> > a Struct ;)
>
> > V = Struct.new(:x, :y).new(nil, nil)
>
> > def bar
> > V.x = 10
> > V.y = 20
> > end
> > def foo
> > bar
> > puts V.x, V.y
> > end
>
> > Regards,
> > Jordan
>
> Of course the Struct is not actually allowing him to share data between
> bar and foo. It's the global constant V that's doing that.
> --
> Posted viahttp://www.ruby-....

Yeah. I thought it fit the desired behavior though, since it shortened
the call to bar (as per desired behavior) and only adds four more
chars ("V." twice) over the original example.

Regards,
Jordan

ruby-talk

12/7/2007 4:28:00 PM

0

On Fri, Dec 07, 2007 at 10:06:15PM +0900, Tim Hunter wrote:
> Hmmm...local variables are called local because they're local to the
> method that uses them. A variable that's shared between two methods
> isn't local.
>
> If foo and bar are both methods in the same class, use instance
> variables. If they're not both in the same class, use global variables.

Do they need to be "variables"?


def bar(arg1,arg2)
klass=class << self; self; end
klass.send(:define_method,:x) { arg1 }
klass.send(:define_method,:y) { arg2 }
end
def foo
bar(42,23)
puts x,y
end
foo

Of course, x and y continue to exist after the scope of foo, but with a
little more wrapping, you could solve that, too. You could create
setters, but they'd have to be accessed like "self.x=".