Class#aliases?

Does this method exist? I think that it doesn’t, but I just thought
I’d make sure.


Giles B.

Blog: http://gilesbowkett.blogspot.com
Portfolio: http://www.gilesgoatboy.org
Tumblelog: http://giles.tumblr.com/

Giles B. wrote:

Does this method exist? I think that it doesn’t, but I just thought
I’d make sure.


Giles B.

Blog: http://gilesbowkett.blogspot.com
Portfolio: http://www.gilesgoatboy.org
Tumblelog: http://giles.tumblr.com/

It is not necessary, because Classe are objects…

irb(main):001:0> class Otto
irb(main):002:1> def hi
irb(main):003:2> puts “Hi!”
irb(main):004:2> end
irb(main):005:1> end
=> nil
irb(main):006:0> Hugo = Otto
=> Otto
irb(main):007:0> Otto.new.hi
Hi!
=> nil
irb(main):008:0> Hugo.new.hi
Hi!
=> nil

Wolfgang Nádasi-Donner

Giles B. wrote:

Does this method exist? I think that it doesn’t, but I just thought
I’d make sure.


Giles B.

Blog: http://gilesbowkett.blogspot.com
Portfolio: http://www.gilesgoatboy.org
Tumblelog: http://giles.tumblr.com/

Again - it cannot exist. The syntactic element “alias” cannot be applied
to local variables, instance variables, class variables and constants -
because class names are (usually) constants, they can’t have aliases. If
you use normal assignment to constants for aliasing classes a simple
method can be defined…

class Class
def aliases?
Module.constants.find_all{|c|self.equal?(const_get(c))}
end
end

Otto = String
Hugo = String
p String.aliases? # => [“Hugo”, “String”, “Otto”]

Wolfgang Nádasi-Donner

Quoth Giles B.:

Does this method exist? I think that it doesn’t, but I just thought
I’d make sure.


Giles B.

Blog: http://gilesbowkett.blogspot.com
Portfolio: http://www.gilesgoatboy.org
Tumblelog: http://giles.tumblr.com/

Irb says no, but I’d guess you’d have already tried that.

Wolfgang Nádasi-Donner wrote:

Giles B. wrote:

btw - is there a difference between “alias” and “alias_method” if
applied to a method name (I don’t mean that I must use Symbols in
"alias_method)?

Wolfgang Nádasi-Donner

On Oct 10, 9:58 pm, “Giles B.” [email protected] wrote:

Does this method exist? I think that it doesn’t, but I just thought
I’d make sure.

Do you mean that you’d like a list of all aliases defined in a class,
similar to #instance_methods?

T.

On Oct 11, 2:46 am, “Wolfgang Nádasi-Donner” [email protected]
wrote:

end

Otto = String
Hugo = String
p String.aliases? # => [“Hugo”, “String”, “Otto”]

Wolfgang Nádasi-Donner


Posted viahttp://www.ruby-forum.com/.

It seems you and I understood the question in two different ways. Only
Giles can answer this for certain, but I don’t think you’re answering
the question he asked.

I understood the point of Class#aliases to mean not “Are there any
other constants out there that are really the same class object, and
if so, what are they?” but “Are there any methods in this class that
are aliases for other methods, and if so, what are the connections?”

Giles, please explain.

Giles, please explain.


-yossef

I was skimming, and read that as “Giles, please explain yourself.”

Anyway -

Do you mean that you’d like a list of all aliases defined in a class,
similar to #instance_methods?

Actually no. I was looking at an error message that expected #aliases
to exist, and I thought it was weird.

I understood the point of Class#aliases to mean not “Are there any
other constants out there that are really the same class object, and
if so, what are they?” but “Are there any methods in this class that
are aliases for other methods, and if so, what are the connections?”

Nope, it was just a weird error message referencing some internal
code. But both these interpretations are pretty interesting.

Technically, as I learned from Ryan D., you can actually
“alias” a class, even though you can’t alias a class.

Monkey = String
=> String
m = Monkey.new(“asdf”)
=> “asdf”
m.gsub!(/asdf/, “qwerty”)
=> “qwerty”


Giles B.

Blog: http://gilesbowkett.blogspot.com
Portfolio: http://www.gilesgoatboy.org
Tumblelog: http://giles.tumblr.com/

Trans wrote:

On Oct 10, 9:58 pm, “Giles B.” [email protected] wrote:

Does this method exist? I think that it doesn’t, but I just thought
I’d make sure.

Do you mean that you’d like a list of all aliases defined in a class,
similar to #instance_methods?

If so, I brought this up a while back:

http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/196740

Ryan D. came up with a rather interesting solution. :slight_smile:

One problem right off the bat, however, is that the Ruby source would
need to be modified to be more stringent about using true aliases in
order such a method to be accurate.

Regards,

Dan