Method Definitions


#1

Just a little curiosity: If method definitions (def foo; end') were to return a symbol representing the method's name (:foo’ in this case)
rather than just nil, would this be possible?

class Klass
private def foo
“Klass#foo”
end

 def bar
   "Klass#bar"
 end

end

obj = Klass.new
obj.bar -> “Klass#bar”
obj.foo -> private method `foo’ called…

Cheers,
Daniel


#2

Hi –

On Fri, 18 Nov 2005, Daniel S. wrote:

 "Klass#bar"

end
end

obj = Klass.new
obj.bar -> “Klass#bar”
obj.foo -> private method `foo’ called…

Yes; I think that’s one of the main things people who advocate this
want to be able to do (just remembering [I think] from earlier
discussions).

David


#3

David A. Black wrote:

 "Klass#foo"

Yes; I think that’s one of the main things people who advocate this
want to be able to do (just remembering [I think] from earlier
discussions).

David

Could you point me to one of those discussions? I wonder why such a
proposal was turned down.

Cheers,
Daniel


#4

Hi –

On Fri, 18 Nov 2005, Daniel S. wrote:

private def foo
obj.foo -> private method `foo’ called…
was turned down.
Search for “make def return something useful”

David


#5

David A. Black wrote:

to return a symbol representing the method’s name (`:foo’ in this
end
David

Could you point me to one of those discussions? I wonder why such a
proposal was turned down.

Search for “make def return something useful”

David

This seems to be it:

http://groups.google.com/group/comp.lang.ruby/browse_thread/thread/3e665f8b51fa62f4/c5fe597f51fa5f20?q="make+def+return+something+useful"&rnum=1

Though I can’t really find a reason for `def’ not to return a symbol.
The discussion linked to seemed pretty unfocused.

Can anyone give me a good reason not to have `def’ return a symbol?

Cheers,
Daniel


#6

On 11/18/05, Daniel S. removed_email_address@domain.invalid wrote:

discussions).
Could you point me to one of those discussions? I wonder why such a
proposal was turned down.

As a big proponent of making def return something useful, it was turned
down in part because what would def return to be passed into private
with:

class Klass
private def self.foo
“Klass.foo”
end
end

If you simply do :foo, then you would be applying private to Klass#foo
– which isn’t what you want. But returning more than :foo isn’t kosher
either since private doesn’t (at this point) know what to do with it.

I think Matz would be at least partially in favour of this if we could
figure out something useful and lightweight to return that wouldn’t
prevent the above code.

-austin


#7

Daniel S. removed_email_address@domain.invalid writes:

Though I can’t really find a reason for `def’ not to return a
symbol. The discussion linked to seemed pretty unfocused.

Can anyone give me a good reason not to have `def’ return a symbol?

Maybe it would make “load” weird…

class Foo
def bar; end
end

load ‘foo.rb’ # => :bar


#8

I think Matz would be at least partially in favour of this if we could
figure out something useful and lightweight to return that wouldn’t
prevent the above code.

Peter V. may have firgured out the best way. He wrote a
patch to return a struct. You can find it here:

http://rubyforge.org/projects/suby/

under ‘def return struct’

T.


#9

Christian N. wrote:

Maybe it would make “load” weird…

class Foo
def bar; end
end

load ‘foo.rb’ # => :bar

I don’t follow… Can you elaborate?

Cheers,
Daniel


#10

Daniel S. removed_email_address@domain.invalid writes:

Christian N. wrote:

Maybe it would make “load” weird…
class Foo
def bar; end
end

load ‘foo.rb’ # => :bar

I don’t follow… Can you elaborate?

Nevermind, I was wrong about the return value of Kernel#load.