Project

General

Profile

Actions

Feature #3688

closed

redef keyword for method redefinition in Ruby 2.0

Added by sunaku (Suraj Kurapati) over 13 years ago. Updated over 11 years ago.

Status:
Rejected
Target version:
[ruby-core:31699]

Description

=begin
Hello,

I saw Matz's Euroko 2010 keynote1 where he mentioned adding the
ability to redefine methods gracefully in Ruby 2.0. I mean, more
gracefully than this:

class Hash
# save the original method
alias original_initialize initialize

 # redefine the method
 def initialize(*args, &block)
   # do something extra ...

   # call the original method
   __original_initialize__(*args, &block)

   # do something more ...
 end

end

I suggest adding a "redef" keyword to Ruby 2.0 for this purpose:

class Hash
# redefine the method
redef initialize(*args, &block)
# do something extra ...

   # call the original method
   super

   # do something more ...
 end

end

The "redef" keyword should also have a twin "redefine_method"
method for use in block-based metaprogramming:

class Hash
# redefine the method
redefine_method :initialize do |*args, &block|
# do something extra ...

   # call the original method
   super

   # do something more ...
 end

end

Thanks for your consideration.

=end

Actions #1

Updated by wardrop (Tom Wardrop) over 13 years ago

=begin
I came on here to suggest the exact same thing. As someone relatively new to Ruby (got into about 5 months ago), I'm surprised there's no easy way of achieving this given that so many developers seem to do this. It's what makes Ruby's dynamic nature so powerful.

There are numerous ways in which this could be implemented. You could introduce a new special method like super, called 'old' which could either hold a reference to the old implementation, or maybe instead, it might just return the old implementation as a Proc object?

I wouldn't want to see the 'super' method used for this purpose, as it may cause unpredictable behaviour. For example, what happens when you want to call the super class (the next implementation in the inheritance chain), but unexpectedly end up calling the old implementation which you'd thought you'd completely over-written.

I also wouldn't want to see anything like redef which can clutter up classes. There just needs to be some simple, clean way of getting at an overwritten method. A common use case is monkey patching (such a feature as this one would go well with the 'refinements' idea), where you want to prepend or append an operation to an existing method, or where you want to modify the return value.

I trust the Ruby developers and decision makers to come up with the most elegant and appropriate solution.
=end

Actions #2

Updated by wardrop (Tom Wardrop) over 13 years ago

=begin
Just to clarify, I'm not completely against the introduction of 'redef', it's just it would have to serve more a purpose than just allow access to the old implementation of a method. For example, if it became a requirement of the language to use redef when overwriting a non-inherited method, then obviously the introduction of 'redef' is a lot more justifiable.
=end

Updated by lazaridis.com (Lazaridis Ilias) almost 13 years ago

=begin

See related issue #5005 with a suggested implementation of "original"

=end

Actions #4

Updated by naruse (Yui NARUSE) over 12 years ago

  • Project changed from Ruby master to 14
  • Category deleted (core)
  • Target version deleted (3.0)
Actions #5

Updated by naruse (Yui NARUSE) over 12 years ago

  • Project changed from 14 to Ruby master

Updated by shyouhei (Shyouhei Urabe) about 12 years ago

  • Description updated (diff)
  • Status changed from Open to Assigned
  • Assignee set to matz (Yukihiro Matsumoto)

Updated by mame (Yusuke Endoh) over 11 years ago

  • Target version set to 3.0

Updated by matz (Yukihiro Matsumoto) over 11 years ago

  • Status changed from Assigned to Rejected

To eliminate alias_method_chain, we introduced Module#prepend.
There's no chance to add redundant feature in the language.

Matz.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0