[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Can the self object be changed inside a Proc?

Emmanuel Oga

8/22/2007 7:24:00 PM

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!
--
Posted via http://www.ruby-....

1 Answer

Pit Capitain

8/22/2007 7:59:00 PM

0

2007/8/22, Emmanuel Oga <oga_emmanuel_oga@yahoo.com.ar>:
> 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