Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class alias in Ruby

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).

like image 233
AdamNYC Avatar asked Oct 17 '11 15:10

AdamNYC


People also ask

What is a method in Ruby?

A method in Ruby is a set of expressions that returns a value. With methods, one can organize their code into subroutines that can be easily invoked from other areas of their program. Other languages sometimes refer to this as a function. A method may be defined as a part of a class or separately.

How do I use a module in Ruby?

The method definitions look similar, too: Module methods are defined just like class methods. As with class methods, you call a module method by preceding its name with the module's name and a period, and you reference a constant using the module name and two colons.


1 Answers

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 
like image 179
Jörg W Mittag Avatar answered Sep 20 '22 16:09

Jörg W Mittag