From: Robert Klemme Date: 2003-05-20T16:31:05+09:00 Subject: Re: public/protected/private syntax "Mauricio Fern�ndez" schrieb im Newsbeitrag news:20030520062014.GA15138@student.ei.uni-stuttgart.de... > On Tue, May 20, 2003 at 06:08:35AM +0900, Robert wrote: > > > we could then have anonymous method definitions too. Regular methods > > > return a symbol, and anonymous methods return an UnboundMethod > > > > > > private def fred > > > ... > > > end > > > > > > dave = def(a,b) > > > a + b > > > end > > > > > > This would be a step towards rationalizing the proc/method issue. > > Then the only differences between anonymous methods and procs would be > the argument passing semantics and the fact that a method needs to be > bound, am I right? I thought that a proc is bound also: irb(main):001:0> class Foo irb(main):002:1> def get irb(main):003:2> proc { self } irb(main):004:2> end irb(main):005:1> end nil irb(main):006:0> irb(main):007:0* f1 = Foo.new # irb(main):008:0> p1 = f1.get # irb(main):009:0> f2 = p1.call # irb(main):010:0> f2.equal? f1 true irb(main):011:0> irb(main):012:0* m1 = f1.method :get # irb(main):013:0> p2 = m1.call # irb(main):014:0> f3 = p2.call # irb(main):015:0> f3.equal? f1 true irb(main):016:0> irb(main):017:0* p1.public_methods - m1.public_methods ["yield", "binding"] irb(main):018:0> m1.public_methods - p1.public_methods ["unbind"] irb(main):019:0> Now I'm starting to get confused: What was the difference between a Method and a Proc? > Will we want to keep both "yield" and "method" > semantics forever, or will say "yield" be phased out? > What about UnboundMethod instances passed as blocks being automatically > bound to self on #call or yield? Interesting thought. However, I'd keep "yield", because it does not need an explicit reference to the block - and it's kind of at the heart of Ruby... > class Foo > def do(*args) > yield(*args) > end > def bar > puts "Foo#bar" > end > end > > f = def; bar end > > Foo.new.do f # => Foo#bar > > Maybe it's not such a good idea because we'd have to document the > expected arg. semantics for each method receiving a block... But don't we do that already with blocks given to "each", "each_with_index" etc.? > > Maybe an alternative to the UnboundMethod instance (if that's too expensive) > > would be an object id of the would be created UnboundMethod instance that > > could be used to retrieve the instance if needed. But then, what exactly > > might be the operatons we want to perform with an anonymous method - apart > > from invoking it? > > arity Yeah, that's right. robert