# Code Review: SymToProc

**URL:** <https://www.ruby-forum.com/t/code-review-symtoproc/184669>\
**Category:** IronRuby\
**Created:** [March 6, 2010, 5:44am UTC](https://www.ruby-forum.com/t/code-review-symtoproc/184669 "2010-03-06T05:44:39Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![grujildo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/94ad74/32.png) [@grujildo](https://www.ruby-forum.com/u/grujildo)\
**Post date:** [March 6, 2010, 5:44am UTC](https://www.ruby-forum.com/t/code-review-symtoproc/184669/1 "2010-03-06T05:44:39Z")

</div>

tfpt review “/shelveset:SymToProc;REDMOND\tomat”  
Comment :  
Implements Symbol#to\_proc

Tomas

---

<div class="post-metadata">

**Author:** ![grujildo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/94ad74/32.png) [@grujildo](https://www.ruby-forum.com/u/grujildo)\
**Post date:** [March 6, 2010, 5:58am UTC](https://www.ruby-forum.com/t/code-review-symtoproc/184669/2 "2010-03-06T05:58:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![grujildo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/94ad74/32.png) [@grujildo](https://www.ruby-forum.com/u/grujildo)\
**Post date:** [March 6, 2010, 7:58am UTC](https://www.ruby-forum.com/t/code-review-symtoproc/184669/3 "2010-03-06T07:58:42Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![grujildo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/94ad74/32.png) [@grujildo](https://www.ruby-forum.com/u/grujildo)\
**Post date:** [March 6, 2010, 7:42am UTC](https://www.ruby-forum.com/t/code-review-symtoproc/184669/4 "2010-03-06T07:42:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![grujildo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/94ad74/32.png) [@grujildo](https://www.ruby-forum.com/u/grujildo)\
**Post date:** [March 6, 2010, 8:31am UTC](https://www.ruby-forum.com/t/code-review-symtoproc/184669/5 "2010-03-06T08:31:28Z")

</div>

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
