From: blondinet Date: 2008-01-14T02:24:58+09:00 Subject: Re: Callable class with block On 13 jan, 10:56, Todd Benson wrote: > On Jan 12, 2008 11:54 AM, blondinet wrote: > > > > > Hi everyone, > > > I would like to know why the following code does not work : > > ----- > > class A > > > def A.[](*args,&block) > > result = self.new( *args ) > > if block > > yield result > > result.end > > end > > return result > > end > > > def end() > > puts "end" > > end > > > end > > > A[] do |a| > > puts "a #{a.inspect}" > > end > > That is interesting. With 1.8.6, I get results like this... > > irb(main):001:0> class C > irb(main):002:1> def [] > irb(main):003:2> yield > irb(main):004:2> end > irb(main):005:1> end > => nil > irb(main):006:0 c = C.new > => # > irb(main):007:0> c[] > LocalJumpError: no block given << from (irb):3:in `[]' > from (irb):7 > irb(main):008:0> c[] {} > SyntaxError: compile error > (irb):8: syntax error, unexpected '{', expecting $end > c[] {} > ^ > from(irb):8 > irb(main):009:0> c.[] {} > => nil > > Bug maybe? I can't think of a reason why the parser would need to > separate [] from .[] > Hmm... > > Todd Thanks Todd for your analysis. Your example is even striking than mine, and I share your opinion on this strange "feature" : that a block is required and cannot be given with the same syntax seems to me a bit "ugly". Any other opinion ? As a beginner on this forum, I do not know yet what to do with this ...