Forum: IronRuby Code Review: SymToProc

Posted by Tomas Matousek (Guest)
on 2010-03-06 05:44
Attachment: SymToProc.diff (16,6 KB)
(Received via mailing list)
tfpt review "/shelveset:SymToProc;REDMOND\tomat"
  Comment  :
  Implements Symbol#to_proc

Tomas
Posted by Jim Deville (Guest)
on 2010-03-06 05:58
(Received via mailing list)
So we're going to have sym#to_proc available in the core 1.8 
implementation? Other than that question, this looks good.

Now I have to go remove some hacks ;)

JD
Posted by Tomas Matousek (Guest)
on 2010-03-06 07:42
(Received via mailing list)
Yep, we already have other methods from 1.8.7, like instance_exec. The 
advantage of having these in 1.8.6 is that it will work in apps that 
check if to_proc method is present. I guess they might do some eval 
magic if the method is not implemented which slows things down.

Tomas
Posted by Jim Deville (Guest)
on 2010-03-06 07:58
(Received via mailing list)
Probably no eval magic, unless you are thinking of a more advanced 
version of Sym#to_proc than I have been using.

I define it with:

Class Symbol
Def to_proc
Lambda {|x| x.send(self)}
End
End

Sure, it doesn't cover everything, but it hits the 90% cases.
Posted by Tomas Matousek (Guest)
on 2010-03-06 08:31
(Received via mailing list)
Yes, something like this might be quite accurate:

class Symbol
  def to_proc
      Proc.new do |*args|
         raise ArgumentError if args.size == 0
         target = args.delete_at(0)
         target.send(self, *args)
     end
  end
end

instance_exec  on the other hand is also 1.8.7 method and is usually 
emulated by string eval.

Tomas
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.