C
Csaba Henk
The post
http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/47474
tells us that one difference between
def foo
...
yield
...
end
and
def foo(&b)
...
b[...]
...
end
is performance, as "'yield' does not create a proc object, while (&block)
does." (The other is the way arguments are passed to the block, but it's not
important here.)
When I've been reading that post, my first thought was that in the "yield"
case a syntactic object (parse tree of the block) is passed internally
(similarly as in the following scheme example:
(define (foo blk) (eval blk))
(foo '(display "today is almost tomorrow"))
), but no, it can't be so, as the bindings of the block are passed when you
use yield as well (
x=8
def foo
yield
end
foo {x}
=> 8
), so both in the "&b" and in the "yield" cases, a closure is created. The
common sense suggests that closures are simply procedure objects, but here
it's not not as simple: as the benchmark in the above cited post shows,
there is a performance difference between the two solutions.
So what I can imply there are two kind of closures in ruby: proc objects and
lightweight closures used with yield. Now I wonder, what's the difference,
what is the addon that is provided with a proc object but is missing in case
of the "lightweight closures"?
Thanks,
--
Csaba
"There's more to life, than books, you know but not much more..."
[The Smiths]
***
If you want to send me a mail, see it in the mailto link at
http://www.renyi.hu/~ekho/egyelore.html
http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/47474
tells us that one difference between
def foo
...
yield
...
end
and
def foo(&b)
...
b[...]
...
end
is performance, as "'yield' does not create a proc object, while (&block)
does." (The other is the way arguments are passed to the block, but it's not
important here.)
When I've been reading that post, my first thought was that in the "yield"
case a syntactic object (parse tree of the block) is passed internally
(similarly as in the following scheme example:
(define (foo blk) (eval blk))
(foo '(display "today is almost tomorrow"))
), but no, it can't be so, as the bindings of the block are passed when you
use yield as well (
x=8
def foo
yield
end
foo {x}
=> 8
), so both in the "&b" and in the "yield" cases, a closure is created. The
common sense suggests that closures are simply procedure objects, but here
it's not not as simple: as the benchmark in the above cited post shows,
there is a performance difference between the two solutions.
So what I can imply there are two kind of closures in ruby: proc objects and
lightweight closures used with yield. Now I wonder, what's the difference,
what is the addon that is provided with a proc object but is missing in case
of the "lightweight closures"?
Thanks,
--
Csaba
"There's more to life, than books, you know but not much more..."
[The Smiths]
***
If you want to send me a mail, see it in the mailto link at
http://www.renyi.hu/~ekho/egyelore.html