From: Daniel Finnie Date: 2008-05-17T08:14:46+09:00 Subject: Re: Easy way to use "self" in Proc ------=_Part_12885_2072908.1210979687943 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline Hi, It is possible. You need to tell the object to evaluate the proc with self as that object like so: >> t = Proc.new { self.length } => # >> "Furry bunny".instance_eval &t <-- note the ampersand. This means pass the proc as a block. => 11 >> [1, 2, 3].instance_eval &t => 3 Because self is an implicit receiver (that is, if no other reciever is specified, self is the receiver), you can leave off self entirely: >> t = Proc.new { length } => # >> [1, 2, 3].instance_eval &t => 3 >> "Furry bunny".instance_eval &t => 11 Dan On Fri, May 16, 2008 at 6:59 PM, Leon Bogaert wrote: > Hi all, > > I have this piece of code: http://pastie.caboo.se/198524 > I'm trying to use the "self" context of the instanciated class in the > Proc. I've got it working now, but isn't there a nicer way to use the > Proc.new() without having to use the "|obj|" part? > > So instead of: > > t.code = Proc.new { |obj| obj.instance_test() } > > I want to use something like: > t.code = Proc.new { self.instance_test() } > > I've tried some stuff but I can't / don't think I can get it working the > second way. Does anyone have some bright ideas about solving this > problem? > > Thanks in advance! > Leon > -- > Posted via http://www.ruby-forum.com/. > > ------=_Part_12885_2072908.1210979687943--