From: Josh Cheek Date: 2012-02-09T00:30:24+09:00 Subject: Re: Using _ like in Scala? --f46d043be25edd08d804b8759273 Content-Type: text/plain; charset=ISO-8859-1 On Wed, Feb 8, 2012 at 5:58 AM, Robert Klemme wrote: > On Wed, Feb 8, 2012 at 3:43 AM, Josh Cheek wrote: > > On Tue, Feb 7, 2012 at 9:15 AM, Robert Klemme < > shortcutter@googlemail.com>wrote: > > > >> > 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 } > >> > >> How would the above be derived from the line further up? > >> > > The name would have no impact on the language, it is still just a block > > which takes one argument. It exists to help the developer (and be used by > > the editor). > > Hmm... Maybe I wasn't clear enough. I was trying to ask how any > automated mechanism could read > > %w[apple banana orange].each display > > and know it must make > > %w[apple banana orange].each { |display -> fruit| puts fruit } > > from it. I am asking specifically since "puts" is nowhere mentioned > in the line above. So even if the syntax would be allowed there would > have to be some ruling which leads to an interpretation equivalent to > the second line. > > Not sure about other editors, but TextMate knows what context it is in if you write something like `[].each { |e| }` put your cursor before the e and press C-P, it tells you variable.other.block.ruby. So it knows when it is in block paramaters, it would just need to then understand that within this context, if it sees something like `|name -> var|` that "name" is the name of this block, and then just allow for block folding. It actually already does all this https://s3.amazonaws.com/josh.cheek/images/scratch/block_folding.png but instead of showing '...' it could show 'name ...' > > Often examples become more complicated, I often feel that the contents > of a > > block are at a different level of abstraction from the containing code > (the > > block is a "how I do it" but I'd like to just look at a "what I am > doing", > > which is where the name has relevance). > > But examples posted here should be at least realistic so we are not > discussing the wrong use case. In case of printing an Enumerable > "puts enum" is certainly the best option. :-) > > I just used that because it was the example provided by the OP. In this example, for instance https://github.com/JoshCheek/deject/blob/5bc94b05cfca045afebcb447170d9f549b9e4006/lib/deject/object_oriented.rb#L39-43 It would be nice if I could define the block like this `|override_body -> value=nil, &initializer|` Which would then get folded up like this (with visual distinction clues provided by the highlighter) def define_override ivar, meth = self.ivar, self.meth klass.send :define_method, "with_#{meth}" override_body... end That method is pretty easy to understand, but when you expand it, the code of the body is inline with the code that sets up the definition, causing it to take much more effort to understand (you parse/interpret it to determine what is and is not relevant to you). --f46d043be25edd08d804b8759273--