Receiving blocks in Proc?

J

James

p = Proc.new { |*args,&block| }

produces syntax error

p = Proc.new { |*args,&block| }
----------------------^

It appears that Proc objects cannot receive blocks?
 
Y

Yukihiro Matsumoto

Hi,

In message "Re: Receiving blocks in Proc?"
on Fri, 5 Nov 2004 06:23:43 +0900, (e-mail address removed) (James) writes:

|p = Proc.new { |*args,&block| }
|
|produces syntax error
|
|p = Proc.new { |*args,&block| }
|----------------------^
|
|It appears that Proc objects cannot receive blocks?

Until 1.9, yes.

matz.
 
M

Mark Hubbart

p = Proc.new { |*args,&block| }

produces syntax error

p = Proc.new { |*args,&block| }
----------------------^

It appears that Proc objects cannot receive blocks?

Not in 1.8. 1.9 has it; it's slated for 2.0, I believe...

b = lambda{|*args, &blk| args.each{|arg| blk[arg]}}
==>#<Proc:0x00522420@(irb):1>
b.call(1,2,3) {|n| puts "number #{n}"}
number 1
number 2
number 3
==>[1, 2, 3]
[RUBY_VERSION, RUBY_RELEASE_DATE]
==>["1.9.0", "2004-09-17"]


cheers,
Mark
 
M

Mark Hubbart

Hi,

In message "Re: Receiving blocks in Proc?"
on Fri, 5 Nov 2004 06:23:43 +0900, (e-mail address removed) (James) writes:

|p = Proc.new { |*args,&block| }


|
|produces syntax error
|
|p = Proc.new { |*args,&block| }
|----------------------^
|
|It appears that Proc objects cannot receive blocks?

Until 1.9, yes.

Will it eventually be possible to pass the block when using the
bracket-call syntax? ie:

block[...] {|*args| ...}

Right now it gives a parse error. Or would that make it too difficult
for the parser?

thanks,
Mark
 
Y

Yukihiro Matsumoto

Hi,

In message "Re: Receiving blocks in Proc?"

|Will it eventually be possible to pass the block when using the
|bracket-call syntax? ie:
|
| block[...] {|*args| ...}
|
|Right now it gives a parse error. Or would that make it too difficult
|for the parser?

I have no plan to allow this. I have alternative idea, which allows
parentheses instead of brackets. But it still is a vague idea, no
promise.


matz.
 
G

gabriele renzi

Yukihiro Matsumoto ha scritto:
Hi,

In message "Re: Receiving blocks in Proc?"

|Will it eventually be possible to pass the block when using the
|bracket-call syntax? ie:
|
| block[...] {|*args| ...}
|
|Right now it gives a parse error. Or would that make it too difficult
|for the parser?

I have no plan to allow this. I have alternative idea, which allows
parentheses instead of brackets. But it still is a vague idea, no
promise.


(on the notes from James Brown)
please please please please (please please oh oh )

Ok, I'd love this, sorry for the noise :)
 
N

nobu.nokada

Hi,

At Fri, 5 Nov 2004 10:38:43 +0900,
gabriele renzi wrote in [ruby-talk:119146]:
(on the notes from James Brown)
please please please please (please please oh oh )

I tried it but it breaks many code.
 
N

nobu.nokada

Hi,

At Fri, 12 Nov 2004 12:38:37 +0900,
Yukihiro Matsumoto wrote in [ruby-talk:120009]:
|> > I have no plan to allow this. I have alternative idea, which allows
|> > parentheses instead of brackets. But it still is a vague idea, no
|> > promise.

|I tried it but it breaks many code.

Yours was too thorough, I guess. What I was thinking was to allow
calling parentheses only after

* predefined local variables
* explicit method calls (with parentheses)

which should break less code.

Well, it did just those things, IIRC. Yes, "many" was not
correct word, but, even in bundled libraries, tests and
samples, some codes were using the variable of the same name as
a method.
 
G

gabriele renzi

(e-mail address removed) ha scritto:
Hi,

At Fri, 12 Nov 2004 12:38:37 +0900,
Yukihiro Matsumoto wrote in [ruby-talk:120009]:
|> > I have no plan to allow this. I have alternative idea, which allows
|> > parentheses instead of brackets. But it still is a vague idea, no
|> > promise.

|I tried it but it breaks many code.

Yours was too thorough, I guess. What I was thinking was to allow
calling parentheses only after

* predefined local variables
* explicit method calls (with parentheses)

which should break less code.


Well, it did just those things, IIRC. Yes, "many" was not
correct word, but, even in bundled libraries, tests and
samples, some codes were using the variable of the same name as
a method.


sorry if I'm asking a stupid question, I know I am dumb.. but would'nt
be enough to give precedence to a method when called and to the variable
when not explicit called?
I.e.


a=proc {|x| 'proc' }
a # #<Proc:0x02befeb0>
a() # 'proc'
a 10 # 'proc'
def a(x) p 'meth' end
a # #<Proc:0x02befeb0> precedence to var, as now
a() # 'meth' precedence to meth as now
a 10 # 'meth' precedence to meth as now


Maybe this is impossible to do at runtime and when building the ast you
were just making a simple 'text substitution' like
a() => a.call() ?
 
N

nobu.nokada

Hi,

At Fri, 12 Nov 2004 19:23:27 +0900,
gabriele renzi wrote in [ruby-talk:120026]:
sorry if I'm asking a stupid question, I know I am dumb.. but would'nt
be enough to give precedence to a method when called and to the variable
when not explicit called?
I.e.


a=proc {|x| 'proc' }
a # #<Proc:0x02befeb0>
a() # 'proc'
a 10 # 'proc'
def a(x) p 'meth' end
a # #<Proc:0x02befeb0> precedence to var, as now
a() # 'meth' precedence to meth as now
a 10 # 'meth' precedence to meth as now

Yes, my patch implemented as you mentioned. But, for instance,
a code in sample/test.rb:

method = method(m)

`method' is defined at the moment assignment expression
appears, so `method()' in RHS calls that variable which is
defined but not assigned yet.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,111
Latest member
KetoBurn
Top