Class alias in Ruby

AdamNYC picture AdamNYC · Oct 17, 2011 · Viewed 18.1k times · Source

I am developing a new Rails app based on a similar existing one. In my old app, I have Coupon class, which is very similar to Ticket in my new app. I want to reuse all code in Coupon, but with a new class name.

Since refactoring is cumbersome in Rails, I wonder if there is a way to create alias for a class in Ruby (similar to alias for attributes and methods).

Answer

Jörg W Mittag picture Jörg W Mittag · Oct 17, 2011

Classes don't have names in Ruby. They are just objects assigned to variables, just like any other object. If you want to refer to a class via a different variable, assign it to a different variable:

Foo = String