[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Create variables with caller scope

Jari Williamsson

1/18/2008 11:02:00 AM

Is it possible for a method to create new variables that becomes in the
scope of the caller (instead of the scope of the method)? I guess I mean
to get a method to behave much like macros or templates in C/C++.

Something like this:
---
def create_some_variables(varname1, varname2)
# Create the variables and initialize with values
end

create_some_variables("a", "b")
# Display the values of those variables
puts a
puts b
---


mvh/
Jari W.

3 Answers

Jean-François Trân

1/18/2008 12:15:00 PM

0

Jari Williamsson :
> Is it possible for a method to create new variables that
> becomes in the scope of the caller (instead of the scope
> of the method)? I guess I mean to get a method to behave
> much like macros or templates in C/C++.

Why not using a block ?

> Something like this:
> ---
> def create_some_variables(varname1, varname2)
> # Create the variables and initialize with values
> end

def create_some_variables(*args)
# Create object_1 and object_2 and initialize with values
# ...
# pass them to the block
yield object_1, object_2 if block_given?
end

> create_some_variables("a", "b")
> # Display the values of those variables
> puts a
> puts b

create_some_variables(...) do |a,b|
# Display the values of those variables
puts a
puts b

# do something with a and b
# ...
end

-- Jean-Fran=E7ois.

Jari Williamsson

1/18/2008 12:28:00 PM

0

Jean-François Trân wrote:
> Jari Williamsson :
>> Is it possible for a method to create new variables that
>> becomes in the scope of the caller (instead of the scope
>> of the method)? I guess I mean to get a method to behave
>> much like macros or templates in C/C++.
>
> Why not using a block ?

Because the created variables will "only" get block scope, if I
understand your sample code correctly. And calling the
create_some_variables() multiple times would create multiple stacked blocks.

But giving it a bit more thought, I think I should redesign the code a
bit and let the method instead dynamically create a class where all the
created variables are stored and then returned an instance of that class.


Best regards,

Jari Williamsson


>
>> Something like this:
>> ---
>> def create_some_variables(varname1, varname2)
>> # Create the variables and initialize with values
>> end
>
> def create_some_variables(*args)
> # Create object_1 and object_2 and initialize with values
> # ...
> # pass them to the block
> yield object_1, object_2 if block_given?
> end
>
>> create_some_variables("a", "b")
>> # Display the values of those variables
>> puts a
>> puts b
>
> create_some_variables(...) do |a,b|
> # Display the values of those variables
> puts a
> puts b
>
> # do something with a and b
> # ...
> end
>
> -- Jean-François.
>
>

Robert Klemme

1/18/2008 12:37:00 PM

0

2008/1/18, Jari Williamsson <jari.williamsson@mailbox.swipnet.se>:
> Jean-Fran=E7ois Tr=E2n wrote:
> > Jari Williamsson :
> >> Is it possible for a method to create new variables that
> >> becomes in the scope of the caller (instead of the scope
> >> of the method)? I guess I mean to get a method to behave
> >> much like macros or templates in C/C++.
> >
> > Why not using a block ?
>
> Because the created variables will "only" get block scope, if I
> understand your sample code correctly. And calling the
> create_some_variables() multiple times would create multiple stacked bloc=
ks.
>
> But giving it a bit more thought, I think I should redesign the code a
> bit and let the method instead dynamically create a class where all the
> created variables are stored and then returned an instance of that class.

OpenStruct and Hash come to mind. One of those is usually far better
than the hack you attempted initially. Btw, there is another issue
with this: local variables created dynamically in a scope cannot be
used directly (i.e. the same way as if they were defined in the scope
directly):

$ ruby -e 'def f() eval("x=3D1", binding); p x end; f'
-e:1:in `f': undefined local variable or method `x' for main:Object (NameEr=
ror)
from -e:1
13:35:55 /cygdrive/c/SCMws/RKlemme/OPSC_Gold_bas_dev_R1.2_perf/bas
$ ruby -e 'def f() eval("x=3D1", binding); p(eval("x", binding)) end; f'
1
13:36:27 /cygdrive/c/SCMws/RKlemme/OPSC_Gold_bas_dev_R1.2_perf/bas

So you need to use some name passing mechanism anyway. And if you do
that you can use a Hash immediately which is much easier and clearer.

Kind regards

robert

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