From: Robert Klemme Date: 2007-10-12T21:03:35+09:00 Subject: Re: Syntax error when redefining >> operator to take a block 2007/10/12, John Woods : > I'm trying to redefine the >> operator for a particular class such that > it takes a block as its argument. It works if I invoke the redifined >> > operator using "." syntax, but causes a syntax error otherwise. > > This code illustrates my problem: > > class C > def >>(&block) > block.call("inside >>") > end > end > > c = C.new > c.>> { |x| puts x } # outputs "inside >>" > #c >> { |x| puts x } # syntax error, if uncommented > > This is the syntax error reported: > > test.rb:10: syntax error, unexpected '|', expecting '}' > c >> { |x| puts x } # syntax error, if uncommented > ^ > test.rb:10: syntax error, unexpected tIDENTIFIER, expecting kDO or '{' > or '(' > c >> { |x| puts x } # syntax error, if uncommented > ^ > > Any pointers? Thanks. Redefining operator behavior cannot change Ruby's syntax. >> is with a block is just not valid Ruby syntax - as you have seen. robert