Method Definitions

D

Daniel Schierbeck

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
 
D

David A. Black

Hi --

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

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
 
D

Daniel Schierbeck

David said:
Hi --

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

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
 
D

David A. Black

Hi --

David said:
Hi --

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

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.

Search for "make def return something useful"


David
 
D

Daniel Schierbeck

David said:
Hi --

David said:
Hi --

On Fri, 18 Nov 2005, Daniel Schierbeck wrote:

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

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.

Search for "make def return something useful"


David

This seems to be it:

http://groups.google.com/group/comp...0?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
 
A

Austin Ziegler

David said:
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 =3D 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).
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
=09private def self.foo
=09 "Klass.foo"
=09end
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
 
T

Trans

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 Vanbroekhoven 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.
 
D

Daniel Schierbeck

Christian said:
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
 

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

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top