Forum: Ruby Method Definitions

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
5da4c52f43677f395aff5bde775593c2?d=identicon&s=25 Daniel Schierbeck (dasch)
on 2005-11-18 14:08
(Received via mailing list)
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
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 dblack (Guest)
on 2005-11-18 14:39
(Received via mailing list)
Hi --

On Fri, 18 Nov 2005, Daniel Schierbeck 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
5da4c52f43677f395aff5bde775593c2?d=identicon&s=25 Daniel Schierbeck (dasch)
on 2005-11-18 14:54
(Received via mailing list)
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
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 dblack (Guest)
on 2005-11-18 15:00
(Received via mailing list)
Hi --

On Fri, 18 Nov 2005, Daniel Schierbeck wrote:

>>>    private def foo
>>>  obj.foo -> private method `foo' called...
> was turned down.
Search for "make def return something useful"


David
5da4c52f43677f395aff5bde775593c2?d=identicon&s=25 Daniel Schierbeck (dasch)
on 2005-11-18 16:40
(Received via mailing list)
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/brow...

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
31ab75f7ddda241830659630746cdd3a?d=identicon&s=25 halostatue (Guest)
on 2005-11-18 16:43
(Received via mailing list)
On 11/18/05, Daniel Schierbeck <daniel.schierbeck@gmail.com> 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
45196398e9685000d195ec626d477f0e?d=identicon&s=25 transfire (Guest)
on 2005-11-18 16:58
(Received via mailing list)
> 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.
7264fb16beeea92b89bb42023738259d?d=identicon&s=25 chneukirchen (Guest)
on 2005-11-18 19:02
(Received via mailing list)
Daniel Schierbeck <daniel.schierbeck@gmail.com> 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
5da4c52f43677f395aff5bde775593c2?d=identicon&s=25 Daniel Schierbeck (dasch)
on 2005-11-18 19:35
(Received via mailing list)
Christian Neukirchen 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
7264fb16beeea92b89bb42023738259d?d=identicon&s=25 chneukirchen (Guest)
on 2005-11-19 12:44
(Received via mailing list)
Daniel Schierbeck <daniel.schierbeck@gmail.com> writes:

> Christian Neukirchen 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.
This topic is locked and can not be replied to.