From: "Florian Groß" Date: 2005-08-10T10:42:59+09:00 Subject: Re: new block notation (was: Re: ruby-dev summary 26468-26661) murphy wrote: > Murrr... why can't we use do...end here? > > double = do |x| x * 2 end > > But I think do...end and {...} should be synonyms. Two intuitions > disagree - so think again. The idea has its point, but think about this case: > def a() 5 end > 1.times do (a = 5) if true; puts a * 2; 1.times do(a) if false; end; end So it would to a point conflict with the current behavior and it might be confusing in other cases. It would probably also be relatively hard to parse. > Ok, if behaves like a method, just that is has no name, we should look > at method definition syntax rather than block syntax. > > double = def x > x * 2 > end This already works and defines a method x() that does endless recursion. double will be nil. > This is illegal in Ruby 1.8, so it shouldn't break code. > > Surely I forgot something, but to me it looks nice, because > - it looks like def > - it looks like block > - it is not used so far (I hope) > - it is short (enougth; you don't use lambda so much) > - it is readable (in my eyes) To be honest I think the make_counter example (follows) is not that readable, because you can easily loose track and read (start=0) do start += 1 end as an argument list followed by a block. > I'm basically with Daniel Brockman, my version of his counter: > make_counter = do(start=0) do start += 1 end end > counter = make_counter[5] > 5.times { puts counter.call } > (Even if I don't understand why you need this; 0.counter would be > cooler. I don't use lambdas much, maybe because I'm grown with Pascal.) The example was relatively made up, but it shows the usage of two nested lambdas and makes at least some sense. (Higher order functions at work.) > Now, the optic / syntax check: > > [...] > > funs = [ > # compare length, noise, rubyness... > ->(a, b) { a - b }, > fun(a, b) { a - b }, > do(a, b) a + b end, > ] Hm, I think the ones with curly braces are easier to read. Especially the fun one, but I am biased. > search_fun = do x: 0, y: 0, width: @width, height: @height, type: > Types.All, limit: @objects.size, skip: 0, destructive: false, > &block > block ||= do object; true end > # etc. > end I think always supplying the parentheses makes it more readable.