From: Nathaniel Talbott Date: 2001-03-23T11:52:11+09:00 Subject: [ruby-talk:13108] Re: alias method Kenichi Komiya [mailto:kom@mail1.accsnet.ne.jp] wrote: > Hi, > > From: "Nathaniel Talbott" > > > One off topic question is, why isn't alias_method() named copy_method? Seems > > clearer about what it actually does. > > Because what alias actually does is making an alias :-) > > As far as alias is concerned, you can view method definition > just like assigning proc objects to variables. > > foo = proc { Dave() } # def foo; Dave(); end > bar = foo # alias bar foo > foo = proc { Fred() } # def foo; Fred(); end > bar[] # => Dave > > I guess what you had in mind was something like below. > > foo[0] = { Dave() } > bar = foo # alias bar foo > foo[0] = { Fred() } > bar[0][] # => Fred > > You see the alias part works in the same way, but end result is > diffetent. Ah, I see. If we think of the class as just holding a reference to a method instance, alias_method is just adding a new method reference to the class and pointing it at the same method instance as the original reference. Changing what the original reference points at obviously doesn't change what the new reference points at. Now that I understand that, the current behavior makes sense, but it certainly doesn't seem obvious at first blush. Dave & Andy, do you think it would be valuable to clarify this in the book? It still seems that, whatever it's doing underneath, from a usage standpoint the current alias_method() is copying the method, since I don't think there's a way to change the original method such that it affects the 'alias'. Is this true? > BTW, one common apprication of alias is inserting extra code > into existing method. > > Something like.... > > class String > alias __to_s__ to_s > > def to_s > "``" + __to_s__ + "''" > end > end > > This will end up infinite recursion if redefining to_s will > change __to_s__ as well. I certainly don't mean to imply that this behavior is bad; rather, it seems extremely useful. I'm just wondering about the naming, and whether, as Jim suggested, it's useful to have a way to create a 'true' alias. I'm certainly happy with just overriding method_missing() when I want to do these types of things, but it might be valuable to have a builtin facility for it, too. Nathaniel <:((>< + - - + - - | RoleModel Software, Inc. & | EQUIP VI | The XP Software Studio(TM) |