instance_eva()l-ing a proc with parameters

  • Thread starter Gennady Bystritsky
  • Start date
G

Gennady Bystritsky

Dear friends,

I need to evaluate a proc within an instance context and would like to
pass it a parameter. The only straight forward way I found is to use
instance_eval(), however it does not allow to pass parameters to a proc,
only the instance itself is passed as a single parameter:

[rh-30-x86.irvspxl08:1004]gbystrit> irb
irb(main):001:0> [ VERSION, PLATFORM ]
=3D> ["1.8.4", "i686-linux"]
irb(main):002:0> block =3D proc { |*_args| puts _args.inspect }
=3D> #<Proc:0xb75b21fc@(irb):2>
irb(main):003:0> class A; end
=3D> nil
irb(main):004:0> a =3D A.new
=3D> #<A:0xb75a9674>
irb(main):005:0> a.instance_eval &block
[#<A:0xb75a9674>]
=3D> nil
irb(main):006:0> a.instance_eval 1, 2, &block # Hypothetical
ArgumentError: wrong number of arguments (2 for 0)
from (irb):6:in `instance_eval'
from (irb):6
from :0

Is there any easy way to do what I want? I also found that I can use
define_method() to turn a proc into an instance method and then call it
with parameters, however it involves a lot of hassle (for one,
define_method() is private).=20

Any thoughts?

Thank you,
Gennady Bystritsky.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top