Hacker News new | ask | show | jobs
by rontr 6681 days ago
If Symbol#to_proc is so great, I'd like to see how it can elegantly express something that can't be hidden behind a simple library function. In any decent language the programmer would write "sum(1,100)". Even if the function's implementation in Python/Java/C++/Smalltalk/Lisp/etc required a few more characters, it wouldn't be a meaningful difference. Does Symbol#to_proc add any real power to Ruby or beyond enabling such linguistic brain teasers?
2 comments

Yes, a library function might be similar, but you would have to write it. s#t_p is already there, so even if it's not as clear, it's all you need.

Did you even read his article? The whole point was "Symbol#to_proc is a litmus test for how you view programming languages. I like it. What do you think of it?"

Your question takes my breath away. May I quote you?
Only if you first answer it.
There are so many answers! Here are a few:

The article does not claim that (1..100).inject(&:+) is great. It claims that it is a totemic example of Ruby code.

There is nothing that technique X can do that technique Y cannot do given that X and Y are both Turing Equivalent. Of course you can write a library function to sum numbers. Do you also need a library function for the product of numbers?

If so, you now know of a salient difference between the two approaches. One needs a function for each thing you want to do, the other gives you some pieces you combine in different ways to do what you want. Is one better than the other? That's up to you to decide, you are the programmer. Ruby lets you write your program either way.

Now, Symbol#to_proc doesn't, in my opinion, add what I would call power. That's because you can always use a block or a lambda wherever you would use a symbol. But I may not have written the article well: the powerful and dangerous feature in Ruby is not Symbol#to_proc, it's Open Classes. Symbol#to_proc is an example of something you can do with an Open Class. Something else you can do with an Open Class is Hash#to_proc. Something else is Object#andand, a low-rent version of the Maybe Monad in Haskell.

I consider Open Classes to be powerful and dangerous. Am I advocating them? Of course not, it is up to you to decide whether they are a good idea or not. What I find interesting about them is that Rubyists are pushing the language in new directions with them, like Symbol#to_proc. The idea of a language growing from the fringe is also powerful and dangerous. The fringe is an uneducated, seething mass.