Jean-FraníÐis TrãÏ 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íÐis.
> 
>