From: "Paolo \"Nusco\" Perrotta" Date: 2007-12-19T00:04:59+09:00 Subject: Re: Difference between alias and alias_method On Dec 17, 11:12 pm, "jwmerr...@gmail.com" wrote: > Don't know exactly why, but wrapping alias in a class_eval seems to > fix the problem: > > class_eval("alias old_initialize initialize") > > Using ri, I couldn't even find who exactly alias belongs to (i.e. is > it defined in Kernel, Object, Module, or what, and is it a class > method of one of these?) Hopefully someone else can weigh in and > clear this up. This is because "alias" is not a method - it's a keyword (that trips me up every time I put a comma between the old and new method name, and the compiler complains). You can use "alias" in two contexts. You normally use it in a class definition to alias an instance method of the class. It's not enough that the class is "self" - you really must be inside the class definition. That's why this works: MyClass.class_eval{ alias :to_string :to_s } while this doesn't (at least, not as you might think): MyClass.instance_eval{ alias :to_string :to_s } What happens if you use alias *outside* a class definition instead? When you do that, you're actually aliasing a method of "self" (the current object) to a singleton method: irb(main):001:0> c = Object.new => # irb(main):002:0> c.instance_eval { alias :clazz :class } => nil irb(main):003:0> c.clazz => Object (If you don't know what a "singleton method" is, you can find many threads about the topic on this group. Be advised that this will require you to take a journey into Ruby metaprogramming). As a special case of the above, if "self" is a class, then you're aliasing an existing method to a singleton method of the class - and a singleton method of the class is what we usually call a class method. That's why instance_eval() might surprise you: irb(main):003:0> String.instance_methods[0..1] => ["%", "select"] irb(main):004:0> String.instance_eval { alias :x :instance_methods } => nil irb(main):006:0> String.x[0..1] => ["%", "select"] Bottom line: if you want "alias" to behave as you probably want it to behave, do like Jason suggested and wrap it in a class_eval(). :) Paolo Perrotta Bologna, Italy