Forum: Ruby-core [ruby-trunk - Feature #7998][Open] "def" should return a value (the symbol of the method name) inste

Posted by pmarreck (Peter Marreck) (Guest)
on 2013-03-01 20:28
(Received via mailing list)
Issue #7998 has been reported by pmarreck (Peter Marreck).

----------------------------------------
Feature #7998: "def" should return a value (the symbol of the method 
name) instead of nil
https://bugs.ruby-lang.org/issues/7998

Author: pmarreck (Peter Marreck)
Status: Open
Priority: Normal
Assignee:
Category:
Target version: next minor


The C code that implements Ruby method definitions already creates a 
symbol corresponding to the method name.

I propose that the "def... end" block returns this symbol.

This would allow the following elegant Ruby code:

protected def foo(); end

private def bar(); end

since "protected" and "private" already take symbol arguments.

I estimate that this would be extremely easy to implement while creating 
extremely minimal existing code impact.

I proposed this idea to the #ruby IRC channel on freenode.net and it was 
received very well, they encouraged me to file this ticket! Thank you 
for considering.
Posted by Student Jr (student)
on 2013-03-03 20:14
(Received via mailing list)
Issue #7998 has been updated by Student (Nathan Zook).


+1

While the proposed usage is good, I immediately thought about this:

new_meth_name = module_eval <<-RUBY
 def prefix_#{val}_postfix
   ...
 end
RUBY

----------------------------------------
Feature #7998: "def" should return a value (the symbol of the method 
name) instead of nil
https://bugs.ruby-lang.org/issues/7998#change-37275

Author: pmarreck (Peter Marreck)
Status: Open
Priority: Normal
Assignee:
Category:
Target version: next minor


The C code that implements Ruby method definitions already creates a 
symbol corresponding to the method name.

I propose that the "def... end" block returns this symbol.

This would allow the following elegant Ruby code:

protected def foo(); end

private def bar(); end

since "protected" and "private" already take symbol arguments.

I estimate that this would be extremely easy to implement while creating 
extremely minimal existing code impact.

I proposed this idea to the #ruby IRC channel on freenode.net and it was 
received very well, they encouraged me to file this ticket! Thank you 
for considering.
Posted by Thomas Sawyer (7rans)
on 2013-03-03 21:27
(Received via mailing list)
Issue #7998 has been updated by trans (Thomas Sawyer).


=begin
It has been suggested long long ago. Probably more than once. Here is a 
link to one: https://bugs.ruby-lang.org/issues/3753.

Once we start looking at code like that for a while will we then start 
to think, why not just:

    public foo()
      ...
    end

    protected foo()
      ...
    end

    private bar()
      ...
    end

=end

----------------------------------------
Feature #7998: "def" should return a value (the symbol of the method 
name) instead of nil
https://bugs.ruby-lang.org/issues/7998#change-37276

Author: pmarreck (Peter Marreck)
Status: Open
Priority: Normal
Assignee:
Category:
Target version: next minor


The C code that implements Ruby method definitions already creates a 
symbol corresponding to the method name.

I propose that the "def... end" block returns this symbol.

This would allow the following elegant Ruby code:

protected def foo(); end

private def bar(); end

since "protected" and "private" already take symbol arguments.

I estimate that this would be extremely easy to implement while creating 
extremely minimal existing code impact.

I proposed this idea to the #ruby IRC channel on freenode.net and it was 
received very well, they encouraged me to file this ticket! Thank you 
for considering.
Posted by Guoliang Cao (gcao)
on 2013-03-09 04:45
This is a nice addition indeed and I remember I asked for it too in the
past. One usage could be method annotation like what is done in some
command line library:

desc 'Do something'
def do_something
  ...
end

The above code looks nice but I believe the underlying code involves
using method_added and is fragile when method_added is also used for
other functionalities.

If def returns method name, then a comma is all we need to remove the
meta-programming complexity.

desc 'Do something',
def do_something
  ...
end

I really wish one day this will be added to Ruby.

Regards,
Cao
Posted by gcao (Guoliang Cao) (Guest)
on 2013-03-09 05:14
(Received via mailing list)
Issue #7998 has been updated by gcao (Guoliang Cao).


This is a nice addition indeed and I remember I asked for it too in the
past. One usage could be method annotation like what is done in some
command line library:

desc 'Do something'
def do_something
  ...
end

The above code looks nice but I believe its underlying code involves
using method_added and is fragile when method_added is also used for
other functionalities.

If def returns method name, then a comma is all we need to remove the
meta-programming complexity.

desc 'Do something',
def do_something
  ...
end

I really wish one day this will be added to Ruby.
----------------------------------------
Feature #7998: "def" should return a value (the symbol of the method 
name) instead of nil
https://bugs.ruby-lang.org/issues/7998#change-37412

Author: pmarreck (Peter Marreck)
Status: Open
Priority: Normal
Assignee:
Category:
Target version: next minor


The C code that implements Ruby method definitions already creates a 
symbol corresponding to the method name.

I propose that the "def... end" block returns this symbol.

This would allow the following elegant Ruby code:

protected def foo(); end

private def bar(); end

since "protected" and "private" already take symbol arguments.

I estimate that this would be extremely easy to implement while creating 
extremely minimal existing code impact.

I proposed this idea to the #ruby IRC channel on freenode.net and it was 
received very well, they encouraged me to file this ticket! Thank you 
for considering.
Posted by kernigh (George Koehler) (Guest)
on 2013-03-13 00:33
(Received via mailing list)
Issue #7998 has been updated by kernigh (George Koehler).


=begin
I have a problem with this feature.

 module Frog
   private def self.sound
     "Ribbit!"
   end
 end

With this feature, def..end would return :sound, then private :sound 
would set Frog#sound to private.  This is wrong.  I defined Frog::sound, 
not Frog#sound.  Surprise!  There is no way for private :sound to know 
that I defined a metamethod, not an instance method; because def..end 
only returned a Symbol, not the whole definition.  With this feature, 
"private def" only works for instance methods, not for metamethods.
=end

----------------------------------------
Feature #7998: "def" should return a value (the symbol of the method 
name) instead of nil
https://bugs.ruby-lang.org/issues/7998#change-37547

Author: pmarreck (Peter Marreck)
Status: Open
Priority: Normal
Assignee:
Category:
Target version: next minor


The C code that implements Ruby method definitions already creates a 
symbol corresponding to the method name.

I propose that the "def... end" block returns this symbol.

This would allow the following elegant Ruby code:

protected def foo(); end

private def bar(); end

since "protected" and "private" already take symbol arguments.

I estimate that this would be extremely easy to implement while creating 
extremely minimal existing code impact.

I proposed this idea to the #ruby IRC channel on freenode.net and it was 
received very well, they encouraged me to file this ticket! Thank you 
for considering.
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.