[ruby-core:37586] [Ruby 1.9 - Feature #4254] Allow method transplanting

5 views
Skip to first unread message

Hiroshi Nakamura

unread,
Jun 27, 2011, 5:20:22 PM6/27/11
to ruby...@ruby-lang.org

Issue #4254 has been updated by Hiroshi Nakamura.

Target version changed from 1.9.3 to 1.9.x


----------------------------------------
Feature #4254: Allow method transplanting
http://redmine.ruby-lang.org/issues/4254

Author: Jonas Pfenniger
Status: Open
Priority: Normal
Assignee:
Category: core
Target version: 1.9.x


=begin
Is there a technical reason to not allow re-binding a method from one module to any other module ?

module M
def foo; "foo"; end
end

module N; end
N.send(:define_method, :foo, M.instance_method(:foo)) #=> should not raise

It's like monkey-patching. Powerful, dangerous, but also really useful. It could allow different variations of method_wrap or alias_method_chain that are not possible right now.
=end

--
http://redmine.ruby-lang.org

nahi

unread,
Mar 18, 2012, 5:57:00 AM3/18/12
to ruby...@ruby-lang.org

Issue #4254 has been updated by nahi.

Status changed from Open to Rejected
Assignee set to matz


----------------------------------------
Feature #4254: Allow method transplanting

https://bugs.ruby-lang.org/issues/4254#change-24902

Author: zimbatm
Status: Rejected
Priority: Normal
Assignee: matz
Category: core
Target version: 2.0.0


=begin
Is there a technical reason to not allow re-binding a method from one module to any other module ?

module M
def foo; "foo"; end
end

module N; end
N.send(:define_method, :foo, M.instance_method(:foo)) #=> should not raise

It's like monkey-patching. Powerful, dangerous, but also really useful. It could allow different variations of method_wrap or alias_method_chain that are not possible right now.
=end

--
http://bugs.ruby-lang.org/

nobu (Nobuyoshi Nakada)

unread,
Jun 1, 2012, 1:53:45 AM6/1/12
to ruby...@ruby-lang.org

Issue #4254 has been updated by nobu (Nobuyoshi Nakada).

Description updated
Status changed from Rejected to Assigned

What status is this proposal?

I don't think the original proposal has been rejected, but only other
additional extensions.

Possible choice would be:

(1) keep current behavior, all method transplanting is prohibited.

(2) accept the original proposal only, transplanting is allowed only
between modules but disallowed between class and module/class.

(3) still look for "relaxed" condition, actually same as (1) but leave
this ticket opened.

(4) others.

I think there is no probable "relaxed" versions, though.

----------------------------------------
Feature #4254: Allow method transplanting
https://bugs.ruby-lang.org/issues/4254#change-26947

Author: zimbatm (Jonas Pfenniger)
Status: Assigned
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)

matz (Yukihiro Matsumoto)

unread,
Jun 25, 2012, 2:38:33 AM6/25/12
to ruby...@ruby-lang.org

Issue #4254 has been updated by matz (Yukihiro Matsumoto).


I'd like to allow method transplanting from a module, not a class, to either class or module.
Any objection?

Matz.


----------------------------------------
Feature #4254: Allow method transplanting
https://bugs.ruby-lang.org/issues/4254#change-27401

duerst (Martin Dürst)

unread,
Jun 25, 2012, 2:51:41 AM6/25/12
to ruby...@ruby-lang.org

Issue #4254 has been updated by duerst (Martin Dürst).


matz (Yukihiro Matsumoto) wrote:
> I'd like to allow method transplanting from a module, not a class, to either class or module.
> Any objection?

I think this is a step in the right direction. Both JavaScript and Python allow functions (including what are essentially methods) to be assigned arbitrarily. There are often better ways to achieve the same thing in Ruby, but sometimes, there's not much of an alternative.

[Some of my students were working on a project to automatically convert Python programs to Ruby, and this is a very hard wall we bumped into.]
----------------------------------------
Feature #4254: Allow method transplanting
https://bugs.ruby-lang.org/issues/4254#change-27402

trans (Thomas Sawyer)

unread,
Jun 25, 2012, 11:08:54 AM6/25/12
to ruby...@ruby-lang.org

Issue #4254 has been updated by trans (Thomas Sawyer).


What prevents methods from being transplanted *from* a class?

----------------------------------------
Feature #4254: Allow method transplanting
https://bugs.ruby-lang.org/issues/4254#change-27413

Author: zimbatm (Jonas Pfenniger)
Status: Closed

rosenfeld (Rodrigo Rosenfeld Rosas)

unread,
Jun 25, 2012, 11:47:27 AM6/25/12
to ruby...@ruby-lang.org

Issue #4254 has been updated by rosenfeld (Rodrigo Rosenfeld Rosas).


I guess it wouldn't make much sense and would lead to other developer's confusion as the method may rely on some internal state of an instance of that class...
----------------------------------------
Feature #4254: Allow method transplanting
https://bugs.ruby-lang.org/issues/4254#change-27414

trans (Thomas Sawyer)

unread,
Jun 25, 2012, 1:17:47 PM6/25/12
to ruby...@ruby-lang.org

Issue #4254 has been updated by trans (Thomas Sawyer).


That makes sense. But I am not sure it matters a great deal. Modules too can have interdependent methods and reference instance variables. I get the concept, but ultimately I'd just prefer to have full flexibility and be done with it, rather than having to fuss with the limitations. This is a meta-programming feature and like all such techniques it requires care by the developer.

However, I am also coming at this with very particular use case in mind. I would make little girl squeally noises to be able to define a method that could include a module that would also include it's class methods. Being able to transplant class methods would make that possible (albeit not the most elegant approach). If there were another means of doing that, then I wouldn't care as much about transplanting class methods.

----------------------------------------
Feature #4254: Allow method transplanting
https://bugs.ruby-lang.org/issues/4254#change-27416

saturnflyer (Jim Gay)

unread,
May 24, 2013, 2:06:46 PM5/24/13
to ruby...@ruby-lang.org

Issue #4254 has been updated by saturnflyer (Jim Gay).


=begin
I am interested in this feature although with a different use.

It appears that 2.0.0-p0 allowed methods to be unbound from a module and bound to any object.

I've forked rubyspec and created a failing spec ((<URL:https://github.com/saturnflyer/rubyspec/compare/a4b320efc34c...668f4be5f99852a>))

In 2.0.0-p0 this code works: (({SomeModule.instance_method(:the_method).bind(Object.new)})) but this seems to be broken in 2.1-dev (and is not present in previous versions)

This project uses this feature to temporarily add behavior to an object ((<URL:https://github.com/saturnflyer/casting>)) and Travis CI is running the tests at ((<URL:https://travis-ci.org/saturnflyer/casting/builds/7432955>))

The specs fail on ruby-head ((<URL:https://travis-ci.org/saturnflyer/casting/jobs/7432959>)) but pass for 2.0 ((<URL:https://travis-ci.org/saturnflyer/casting/jobs/7432957>))

This feature is useful in allowing an object to run methods inside a block:


object.hello_world #=> NoMethodError

Casting.delegating(object => GreetingModule) do
object.hello_world #=> "Hello world!"
end

object.hello_world #=> NoMethodError


The above example uses method_missing to unbind methods from the given module then bind them to self and call them.

If there is a better place to post this, or if I should open a new ticket, please correct me.

=end
----------------------------------------
Feature #4254: Allow method transplanting
https://bugs.ruby-lang.org/issues/4254#change-39523

nobu (Nobuyoshi Nakada)

unread,
May 25, 2013, 11:03:24 AM5/25/13
to ruby...@ruby-lang.org

Issue #4254 has been updated by nobu (Nobuyoshi Nakada).


=begin
Seems fine.

$ ruby -v -e 'module M; def foo; :foo;end; end; p m = M.instance_method(:foo).bind(Object.new); p m.call'
ruby 2.1.0dev (2013-05-25 trunk 40923) [universal.x86_64-darwin11]
#<Method: Object(M)#foo>
:foo

Please open new ticket if needed.
=end

----------------------------------------
Feature #4254: Allow method transplanting
https://bugs.ruby-lang.org/issues/4254#change-39529

nobu (Nobuyoshi Nakada)

unread,
May 26, 2013, 9:21:41 AM5/26/13
to ruby...@ruby-lang.org

Issue #4254 has been updated by nobu (Nobuyoshi Nakada).


saturnflyer (Jim Gay) wrote:
> The specs fail on ruby-head ((<URL:https://travis-ci.org/saturnflyer/casting/jobs/7432959>)) but pass for 2.0 ((<URL:https://travis-ci.org/saturnflyer/casting/jobs/7432957>))

From your "ruby-head" log:

$ ruby --version
ruby 2.0.0dev (2012-12-12) [x86_64-linux]

It's too old.

----------------------------------------
Feature #4254: Allow method transplanting
https://bugs.ruby-lang.org/issues/4254#change-39532

saturnflyer (Jim Gay)

unread,
May 31, 2013, 12:53:50 AM5/31/13
to ruby...@ruby-lang.org

Issue #4254 has been updated by saturnflyer (Jim Gay).


thanks nobu! I blindly trusted the travis-ci build. good to know this is still valid behavior.
----------------------------------------
Feature #4254: Allow method transplanting
https://bugs.ruby-lang.org/issues/4254#change-39577
Reply all
Reply to author
Forward
0 new messages