From: Josh Cheek Date: 2012-02-07T21:00:36+09:00 Subject: Re: Using _ like in Scala? --0016e6de00ecb4d5c404b85e86da Content-Type: text/plain; charset=ISO-8859-1 On Tue, Feb 7, 2012 at 4:47 AM, Robert Klemme wrote: > On Tue, Feb 7, 2012 at 6:56 AM, Kevin wrote: > > It would make more sense to just eliminate the need to type even a > > place holder while keeping the current form. I.E. fruits.each{puts} > > Or come up with semantics that allow the interpreter to accept an > > input like "puts fruits" and have Ruby unwind the collection. > > Like > > irb(main):001:0> class Object > irb(main):002:1> def out(io = $stdout) io.puts(self) end > irb(main):003:1> end > => nil > irb(main):004:0> fruits = %w( apple banana orange ) > => ["apple", "banana", "orange"] > irb(main):005:0> fruits.each &:out > apple > banana > orange > => ["apple", "banana", "orange"] > > ? Btw, I don't like _ either. > > I do this sometimes, but I do it like this %w[apple banana orange].each &method(:puts) # >> apple # >> banana # >> orange This one isn't too bad. It can get kind of gross when you need to get the method from elsewhere, though: %w[apple banana orange].each &Kernel.method(:puts) # >> apple # >> banana # >> orange I wouldn't mind if there were cleaner ways of accessing the methods themselves (some kind of syntax). Although what I'd really like would be a syntax for naming inline blocks, then my editor could fold the block body inside the name, something like %w[apple banana orange].each display And it would expand to something like %w[apple banana orange].each { |display -> fruit| puts fruit } Then it looks good if it's in plain text or managed by the editor. The biggest benefit to me is that it allows you to abstract the implementation of what you want to do with this thing from its implementation, but without littering your class with methods only used in one place (also methods aren't closures, so often won't work for this. I've played around with this idea using methods def display lambda { |fruit| puts fruit } end %w[apple banana orange].each &display and using local variables display = lambda { |fruit| puts fruit } %w[apple banana orange].each &display But never found anything I was very happy with. --0016e6de00ecb4d5c404b85e86da--