Calling a block with parameters in a new scope

R

Ruby Talk

Calling a block in a new scope is easy enough:

self.instance_eval &block

And calling a block, passing it parameters is easy enough:

block.call(*params)

But is there some way to do both? To both change the scope of evaluation
and pass variables into the block.
 
M

Matt Gretton

Ruby said:
Calling a block in a new scope is easy enough:

self.instance_eval &block

And calling a block, passing it parameters is easy enough:

block.call(*params)

But is there some way to do both? To both change the scope of evaluation
and pass variables into the block.

Hello,

I'm not sure I fully understand the question being asked here. Can you
provide a small bit of code to highlight what you are trying to acheive.

Do you wish to pass vaiables into the instance_eval block?

Any variables in scope when the instance_eval block is called can be
used in the instance_eval block itself. However, if you were to pass a
proc object into the instance_eval it would retain the context it was
created at.

See below for an example of this.

class Test
def initialize(arg)
@instance_variable = arg
end
end

test_obj = Test.new("initializing_arg")

outside = "outside instance_eval"

proc = Proc.new {|arg| puts self} #self main here

test_obj.instance_eval do
puts @instance_variable #test_obj instance variable.
puts outside #any variable from outside the block can be passed in.
puts proc #will return main rather than the object
end

Hmm, I get the feeling I have ranted about stuff that does not help you
one bit with your question...

Get back to me with a bit more detail and hopefully I'll be able to
help!

Cheers,

Matt.
 
M

Matt Gretton

Matt said:
Hello,

I'm not sure I fully understand the question being asked here. Can you
provide a small bit of code to highlight what you are trying to acheive.

Do you wish to pass vaiables into the instance_eval block?

Any variables in scope when the instance_eval block is called can be
used in the instance_eval block itself. However, if you were to pass a
proc object into the instance_eval it would retain the context it was
created at.

See below for an example of this.

class Test
def initialize(arg)
@instance_variable = arg
end
end

test_obj = Test.new("initializing_arg")

outside = "outside instance_eval"

proc = Proc.new {|arg| puts self} #self main here

test_obj.instance_eval do
puts @instance_variable #test_obj instance variable.
puts outside #any variable from outside the block can be passed in.
puts proc #will return main rather than the object
end

Hmm, I get the feeling I have ranted about stuff that does not help you
one bit with your question...

Get back to me with a bit more detail and hopefully I'll be able to
help!

Cheers,

Matt.


Oops, that last 'puts proc' should be proc.call("whatever")

If full:

class Test
def initialize(arg)
@instance_variable = arg
end
end

test_obj = Test.new("initializing_arg")

outside = "outside instance_eval"

proc = Proc.new {|arg| puts self} #self main here

test_obj.instance_eval do
puts @instance_variable #test_obj instance variable.
puts outside #any variable from outside the block can be passed in.
proc.call("whatever") #will return main rather than the object
end
 

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,774
Messages
2,569,599
Members
45,169
Latest member
ArturoOlne
Top