Can the self object be changed inside a Proc?

E

Emmanuel Oga

Hello, basically i want to change the self object inside a proc so i can
do a thing like this:

the_proc= lambda { @variable }

class Test1
def initialize proc
@variable= 1
# Here i need to bind somehow the proc to the instance of Test1
puts proc.call # should emit 1 from @variable
end
end

class Test2
def initialize proc
@variable= 2
# Here i need to bind somehow the proc to the instance of Test2
puts proc.call # should emit 2 from @variable
end
end

Test1.new
Test2.new

That's only an example. The real need is because of a rails application
in wich i pass a proc that is going to be evaluated in one of the
controller's helpers:

customers_link_builder= lambda do |resource, helper|
helper.instance_eval do
link_to "Ver clientes", customers_path(resource)
end
end

If I could bind the proc to the helper i could do better like this:

customers_link_builder= lambda do |resource|
link_to "Ver clientes", customers_path(resource)
end

That would be very nice!!!

Thank you!
 
P

Pit Capitain

2007/8/22 said:
Hello, basically i want to change the self object inside a proc so i can
do a thing like this:

the_proc= lambda { @variable }

class Test1
def initialize proc
@variable= 1
# Here i need to bind somehow the proc to the instance of Test1
puts proc.call # should emit 1 from @variable
end
end

(...)

Emmanuel, just change

proc.call

to

instance_eval(&proc)

Regards,
Pit
 

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,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top