[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Callable class with block

blondinet

1/12/2008 5:51:00 PM

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
-----

When I want to execute this script, the interpreter outputs the
following error :
--
d:/DEV/XRVG/bug.rb:18: parse error, unexpected kDO, expecting $
A[] do |a|
--

If I write "A.[] do |a|" instead of "A[] do |a|", it is OK and
outputs:
----
a #<A:0x28ee264>
end
----

Any idea ?
Any help appreciated !! Thanks in advance.
13 Answers

ara.t.howard

1/12/2008 9:01:00 PM

0


On Jan 12, 2008, at 10:54 AM, blondinet wrote:

> Any idea ?

[] method can't take block unless you do

A.send '[]' do
end

a @ http://draw...
--
sleep is the best meditation.
h.h. the 14th dalai lama




blondinet

1/12/2008 10:00:00 PM

0

On 12 jan, 22:00, "ara.t.howard" <ara.t.how...@gmail.com> wrote:
> On Jan 12, 2008, at 10:54 AM, blondinet wrote:
>
> > Any idea ?
>
> [] method can't take block unless you do
>
> A.send '[]' do
> end
>
> a @http://draw...
> --
> sleep is the best meditation.
> h.h. the 14th dalai lama

Thanks for your response. However, I do not really understand why such
a behavior : for me, Class[] is just a shortcut for Class.[], which is
just a shortcut for Class.send '[]' ... Where am I wrong ?

Tim Hunter

1/12/2008 10:06:00 PM

0

blondinet wrote:
> On 12 jan, 22:00, "ara.t.howard" <ara.t.how...@gmail.com> wrote:
>> On Jan 12, 2008, at 10:54 AM, blondinet wrote:
>>
>>> Any idea ?
>> [] method can't take block unless you do
>>
>> A.send '[]' do
>> end
>>
>> a @http://draw...
>> --
>> sleep is the best meditation.
>> h.h. the 14th dalai lama
>
> Thanks for your response. However, I do not really understand why such
> a behavior : for me, Class[] is just a shortcut for Class.[], which is
> just a shortcut for Class.send '[]' ... Where am I wrong ?
>

Ruby's parser doesn't allow a block to follow [].


--
RMagick: http://rmagick.ruby...
RMagick 2: http://rmagick.ruby...rmagick2.html

blondinet

1/13/2008 9:23:00 AM

0

On 12 jan, 23:06, Tim Hunter <TimHun...@nc.rr.com> wrote:
> blondinet wrote:
> > On 12 jan, 22:00, "ara.t.howard" <ara.t.how...@gmail.com> wrote:
> >> On Jan 12, 2008, at 10:54 AM, blondinet wrote:
>
> >>> Any idea ?
> >> [] method can't take block unless you do
>
> >> A.send '[]' do
> >> end
>
> >> a @http://draw...
> >> --
> >> sleep is the best meditation.
> >> h.h. the 14th dalai lama
>
> > Thanks for your response. However, I do not really understand why such
> > a behavior : for me, Class[] is just a shortcut for Class.[], which is
> > just a shortcut for Class.send '[]' ... Where am I wrong ?
>
> Ruby's parser doesn't allow a block to follow [].
>
> --
> RMagick:http://rmagick.ruby...
> RMagick 2:http://rmagick.ruby...rmagick2.html

Hello Tim, thank you for your response. However I think it is a bit
erroneous, since in my example, "A.[] do |a|" works !!
Anyway, I will go with it, even if it is the first time that I find
Ruby a bit incoherent, and I would like to know why ... (sigh)

P.S : by the way, Tim, thanks for your work on RMagick, it is quite
helpfull !!
You may find interesting the following link http://xrvg.ruby...,
even if it just a beginning :-(

Todd Benson

1/13/2008 9:57:00 AM

0

On Jan 12, 2008 11:54 AM, blondinet <jblondinet@yahoo.com> 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
=> #<C:0x2dfac1c>
irb(main):007:0> c[]
LocalJumpError: no block given <<<interesting part
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

blondinet

1/13/2008 5:22:00 PM

0

On 13 jan, 10:56, Todd Benson <caduce...@gmail.com> wrote:
> On Jan 12, 2008 11:54 AM, blondinet <jblondi...@yahoo.com> 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
> => #<C:0x2dfac1c>
> irb(main):007:0> c[]
> LocalJumpError: no block given <<<interesting part
> 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 ...

Robert Dober

1/13/2008 7:16:00 PM

0

On Jan 13, 2008 6:24 PM, blondinet <jblondinet@yahoo.com> wrote:
> On 13 jan, 10:56, Todd Benson <caduce...@gmail.com> wrote:
>
> > On Jan 12, 2008 11:54 AM, blondinet <jblondi...@yahoo.com> 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
> > => #<C:0x2dfac1c>
> > irb(main):007:0> c[]
> > LocalJumpError: no block given <<<interesting part
> > 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 ...
>
>
As this behavior is inherited in 1.9 too :(, I'd say somebody cross
posting this to ruby-core maybe.
Personally I'd love to have that changed.

Cheers
Robert

--
http://ruby-smalltalk.blo...

---
Whereof one cannot speak, thereof one must be silent.
Ludwig Wittgenstein

Lars Christensen

1/14/2008 2:58:00 PM

0

On Jan 13, 8:16 pm, Robert Dober <robert.do...@gmail.com> wrote:
> Personally I'd love to have that changed.

[] is an operator (the index operator). I would expect [] to have
higher precedence than anything that follows it. Although implemented
as such, it should not be seen as a function call, IMHO.

If

foo[i] #=> 42

Then I would expect

foo[i] { }

to be the same as

42 { }

which makes little sense.

Todd Benson

1/14/2008 3:25:00 PM

0

On Jan 14, 2008 9:01 AM, Lars <larsch@belunktum.dk> wrote:
> On Jan 13, 8:16 pm, Robert Dober <robert.do...@gmail.com> wrote:
> > Personally I'd love to have that changed.
>
> [] is an operator (the index operator). I would expect [] to have
> higher precedence than anything that follows it. Although implemented
> as such, it should not be seen as a function call, IMHO.
>
> If
>
> foo[i] #=> 42
>
> Then I would expect
>
> foo[i] { }
>
> to be the same as
>
> 42 { }
>
> which makes little sense.

So operators that you can redefine and execute as a method cannot
except a block as a general rule of thumb?

I would think foo[i] { } would be parsed as (foo[i] { }) as a single
piece, especially if you defined it to receive a block.

fedzor

1/14/2008 3:36:00 PM

0


On Jan 14, 2008, at 10:25 AM, Todd Benson wrote:

> So operators that you can redefine and execute as a method cannot
> except a block as a general rule of thumb?
>
> I would think foo[i] { } would be parsed as (foo[i] { }) as a single
> piece, especially if you defined it to receive a block.
>


Ruby is smart so you don't have to be. But here, it works against us.

foo.[](i) do
puts 42
end

Mess around with removing the dot, putting the i in between the
brackets, etfc.