3 ms·
Object#returning (Ruby's K Combinator)
- pius 18y agoIt's (perhaps) worth noting that Object#returning is part of ActiveSupport, not the Ruby Standard Library.
- raganwald 18y agoTrue, and worth noting. Also of interest: Ruby 1.9 includes a different implementation of the K Combinator, Object#tap. It's a method rather than a pseudo-function, so instead of: returning (Fubar.new) do |f| ... end Your write: Fubar.new.tap do |f| ... end Shameless plug-whoring: The andand gem includes an enhanced Object#tap: http://andand.rubyforge.org http://andand.rubyforge.org
- ionfish 18y agoI had a couple of spare minutes so I did a JavaScript implementation. http://gist.github.com/7727 http://gist.github.com/7727
- Harkins 18y agoAlso see http://kcombinator.com http://kcombinator.com
- eru 18y agoSide-effects are evil!
- raganwald 18y agoHere's a use for the K Combinator that isn't Your Father's Imperative Programming. I sometimes want an arbitrary, one-off object when testing or prototyping things, perhaps in irb: foo = returning(Object.new) do |f| def f.bar ... end def f.bash ... end end
- zenspider 18y agofoo = returning(Object.new) do |f| def f.bar ... end def f.bash ... end end # vs foo = Object.new def foo.bar ... end def foo.bash ... end 1 line shorter, less indentation, more clear. What does returning get you besides another message send and block activation? I have yet to see a good use for it. (yes, I'm biased. calibrate accordingly)
- raganwald 18y agoIt really depends. If the definitions of foo.bar and foo.bash make sense independantly of the assignment to foo, I prefer the second form as well. If in my brain the whole thing is "atomic", I prefer the returning form or to make a helper method so that they are composed into a logical chunk. For the same reason, I write: foo = bar bash = blitz When teh two statements are independant, and: foo, bar = bash, blitz When they are deeply related and should not be broken up. Sometimes that is hard to read and I will get gratuitous: begin foo = bar bash = blitz end What can I say... that uses more characters than needed. It isn't always about saving electrons for me.
- rob_rasmussen 18y agoFor a different perspective on Object#returning, (and Symbol#to_proc and friends), see http://wiki.merbivore.com/pages/it-is-a-bug-if http://wiki.merbivore.com/pages/it-is-a-bug-if merb is one of the best-written Ruby libraries I've seen, in large part because they're focused on avoiding clever code.
- raganwald 18y agoI agree that it is a bug to use Symbol#to_proc or Object#returning in merb, however I suggest that neither of these things are "clever." Instead, I note that neither of these things are part of the standard library today. Which means, you must open the Object or Symbol classes to implement them. Nothing wrong with that in your code, but when you do so in a framework like merb (or Rails!), everyone using your framework gets your little patches in their namespace as well. My feeling is that with the way Ruby's global class namespace works today, opening a core class has much more serious consequences when you are writing a framework, gem, or plug-in then when you are writing code that does not have "downstream clients."
- stcredzero 18y agoYup. Everything shoved into the dictionary named Smalltalk, including "isA" methods for every subsystem you've written shoved up to Object isn't a big deal for in-house development. When you publish a library or grab one from somewhere, then it becomes a big problem. Then you want namespaces. So the real-world experience for Smalltalk is that the former is mostly FUD. The latter is a real issue. I suspect that this pretty much applies to the other dynamic languages as well.
- zenspider 18y agoWell, that's partially false. #to_proc is in 1.9. I also completely and totally disagree that they're not clever. There is nothing _not_ clever about #returning. It is second only to using inject because you're too lazy to assign to a variable first. Nevermind that every railz0r that does that ignores the fact that they've now assigned N times instead of once. Clever code, in my mind, is a form of hidden cost. It seems to be what I get paid to deal with (read: remove) the most.
- deleted 18y ago[deleted]