Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Is Passing a Binding with Parameters Possible?

Reply
Thread Tools

Is Passing a Binding with Parameters Possible?

 
 
John Sikora
Guest
Posts: n/a
 
      09-03-2009
Does anyone know how to pass a binding using parameters instead of a
code block? Is it even possible?

For various reasons (primarily syntactic sugar implementation), I need
to eval a string in one method in the scope of another method. The
following is a simplified version of the code (which works):


def called_method(*parameters, &code_block) # code_block handler
ignored.
parameters.each do |parameter|
eval(parameter, code_block.binding)
end
end

def calling_method
a = 3
string_to_evaluate = 'puts a'
called_method(string_to_evaluate){}
end

calling_method # => 3


Obviuosly, this requires a code block for 'called_method'. In the actual
code, whenever eval is used, parameters are always present. Code blocks
are optional. So when a code block is not needed, the empty code block
has to be included.

No big deal, but it would be nice to be able to use the parameters to
pass the binding (since they are always present when eval is used). Such
as:


def called_method(*parameters, &code_block) # code_block handler
ignored.
parameters.each do |parameter|
eval(parameter, parameters.binding)
end
end

def calling_method
a = 3
string_to_evaluate = 'puts a'
called_method(string_to_evaluate)
end

calling_method # => in 'called_method' private method 'binding'
called...


So I guess .binding is a private method for objects other than code
blocks, in this case an Array. If I change 'parameters.binding' to
'binding', it (of course) does not give the right binding, resulting in
variable 'a' being undefined. I have tried some other code snippets that
I thought might work, but to no avail.

I know a binding could be created inside of calling_method and it could
be could be passed, but the way it is now with the empty braces is
easier, which is the point of syntactic sugar.

Can it be made even easier by passing the binding 'automatically' with
the parameters instead of a code block?

John S.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
7stud --
Guest
Posts: n/a
 
      09-04-2009
John Sikora wrote:
>
> Obviuosly, this requires a code block for 'called_method'. In the actual
> code, whenever eval is used, parameters are always present. Code blocks
> are optional. So when a code block is not needed, the empty code block
> has to be included.
>
> No big deal, but it would be nice to be able to use the parameters to
> pass the binding


How about:

def called_method(*parameters, &code_block)
if block_given?
#do something

elsif
b = parameters[-1]
params = parameters[0..-2]

params.each do |param|
eval(param, b)
end

end
end

def calling_method
a = 3
string_to_evaluate = 'puts a'
called_method(string_to_evaluate, binding)
end

calling_method # => 3

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

 
Reply With Quote
 
 
 
 
7stud --
Guest
Posts: n/a
 
      09-04-2009
Or this:

class Parameters
def initialize(bind_ing, *parameters)
@binding = bind_ing
@parameters = parameters
end

def each
@parameters.each {|param| yield [param, @binding]}
end

end

def called_method(parameters, &code_block)
if block_given?
#do something

elsif
parameters.each do |param, bind_ing|
eval(param, bind_ing)
end

end
end

def calling_method
a = 3
b = 4
eval_str1 = 'puts a'
eval_str2 = 'puts b'
params = Parameters.new(binding, eval_str1, eval_str2)
called_method(params)
end

calling_method # => 3, 4

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

 
Reply With Quote
 
Space Ship Traveller
Guest
Posts: n/a
 
      09-04-2009
[Note: parts of this message were removed to make it a legal post.]

I like lmgtfy:

http://lmgtfy.com/?q=Binding+of_caller

Kind regards,
Samuel

On 4/09/2009, at 9:03 AM, John Sikora wrote:

> Does anyone know how to pass a binding using parameters instead of a
> code block? Is it even possible?
>
> For various reasons (primarily syntactic sugar implementation), I need
> to eval a string in one method in the scope of another method. The
> following is a simplified version of the code (which works):
>
>
> def called_method(*parameters, &code_block) # code_block handler
> ignored.
> parameters.each do |parameter|
> eval(parameter, code_block.binding)
> end
> end
>
> def calling_method
> a = 3
> string_to_evaluate = 'puts a'
> called_method(string_to_evaluate){}
> end
>
> calling_method # => 3
>
>
> Obviuosly, this requires a code block for 'called_method'. In the
> actual
> code, whenever eval is used, parameters are always present. Code
> blocks
> are optional. So when a code block is not needed, the empty code block
> has to be included.
>
> No big deal, but it would be nice to be able to use the parameters to
> pass the binding (since they are always present when eval is used).
> Such
> as:
>
>
> def called_method(*parameters, &code_block) # code_block handler
> ignored.
> parameters.each do |parameter|
> eval(parameter, parameters.binding)
> end
> end
>
> def calling_method
> a = 3
> string_to_evaluate = 'puts a'
> called_method(string_to_evaluate)
> end
>
> calling_method # => in 'called_method' private method 'binding'
> called...
>
>
> So I guess .binding is a private method for objects other than code
> blocks, in this case an Array. If I change 'parameters.binding' to
> 'binding', it (of course) does not give the right binding, resulting
> in
> variable 'a' being undefined. I have tried some other code snippets
> that
> I thought might work, but to no avail.
>
> I know a binding could be created inside of calling_method and it
> could
> be could be passed, but the way it is now with the empty braces is
> easier, which is the point of syntactic sugar.
>
> Can it be made even easier by passing the binding 'automatically' with
> the parameters instead of a code block?
>
> John S.
> --
> Posted via http://www.ruby-forum.com/.
>



 
Reply With Quote
 
John Sikora
Guest
Posts: n/a
 
      09-04-2009
7stud -- wrote:
> Or this:
>


> def calling_method
> a = 3
> b = 4
> eval_str1 = 'puts a'
> eval_str2 = 'puts b'
> params = Parameters.new(binding, eval_str1, eval_str2)
> called_method(params)
> end
>
> calling_method # => 3, 4


7stud,

I was hoping to be able to do it w/o having to include the binding (in
any form)in calling_method. In other words, the binding could be
'attached' to the parameters similar to a code block.

Thanks,
John S.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
John Sikora
Guest
Posts: n/a
 
      09-04-2009
Space Ship Traveller wrote:
> I like lmgtfy:
>
> http://lmgtfy.com/?q=Binding+of_caller
>
> Kind regards,
> Samuel



SST,

Thanks for the link. No one has ever accused me of being particularly
adept at conducting a web search.

Unfortunately, I feel even more ignorant because I cannot get the code
to work. At first I tried adapting the code in my post to use
Binding.of_caller. When it did not work, I tried the example code from
the link:


require
'c:/Ruby/lib/ruby/gems/1.8/gems/extensions-0.6.0/lib/extensions/binding'

def inc_counter
Binding.of_caller do |b|
eval("counter += 1", b)
end
end

counter = 0
inc_counter
inc_counter
puts counter


I get an error message that says: in 'inc_counter': undefined method '+'
for nil:NilClass. So it is not getting the correct binding. Debugging
shows that Binding.of_caller returned a binding to inc_counter.

The code in the require file matches the source code of of_caller in the
link, so at least I know that is right.

Is there something obvious am I doing wrong? I am running Ruby 1.8.6 on
Windows. I will try to educate myself by studying the source code to
figure out what is wrong, but it will take some time (if I ever get
there).

Thanks,
John S.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Caleb Clausen
Guest
Posts: n/a
 
      09-05-2009
On 9/4/09, John Sikora <(E-Mail Removed)> wrote:
> Space Ship Traveller wrote:
>> I like lmgtfy:
>>
>> http://lmgtfy.com/?q=Binding+of_caller

>
> Thanks for the link. No one has ever accused me of being particularly
> adept at conducting a web search.
>
> Unfortunately, I feel even more ignorant because I cannot get the code
> to work. At first I tried adapting the code in my post to use


Binding.of_caller is really a hack... Don't use it. You're better off
passing a binding explicitly, like 7stud says, or implicitly in a
block, like you had originally.

 
Reply With Quote
 
John Sikora
Guest
Posts: n/a
 
      09-08-2009
Caleb Clausen wrote:
> On 9/4/09, John Sikora <(E-Mail Removed)> wrote:
>> Space Ship Traveller wrote:
>>> I like lmgtfy:
>>>
>>> http://lmgtfy.com/?q=Binding+of_caller

>>
>> Thanks for the link. No one has ever accused me of being particularly
>> adept at conducting a web search.
>>
>> Unfortunately, I feel even more ignorant because I cannot get the code
>> to work. At first I tried adapting the code in my post to use

>
> Binding.of_caller is really a hack... Don't use it. You're better off
> passing a binding explicitly, like 7stud says, or implicitly in a
> block, like you had originally.


Caleb,

Thanks, I'll just leave it as is.

John S.
--
Posted via http://www.ruby-forum.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
Passing parameters to an executable vs. passing them to a server Ramon F Herrera C++ 8 09-13-2009 02:48 AM
GridView binding - how to stop initial binding Amit ASP .Net 6 10-24-2006 08:06 AM
pywin32 COM sort in Excel (late binding fails, early binding works) (+py2exe) kogrover@gmail.com Python 2 10-20-2006 04:08 PM
Complex data binding question, binding child objects of a custom collection. JcFx ASP .Net Datagrid Control 0 06-01-2005 04:01 PM
Data Binding - using inline code vs. functions vs. straight binding Jordan ASP .Net 2 02-10-2004 08:32 PM



Advertisments