Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I name a Rails ruby file whose class name has numbers?

Can a Rails class name contain numbers? For example:

class Test123
end

Is this a valid class? I get an uninitialized constant Test123 error when I try to load the class.

like image 423
Artem Kalinchuk Avatar asked Mar 28 '12 14:03

Artem Kalinchuk


People also ask

How do I name a Ruby file?

The word from Ruby Gems is to use underscores (a.k.a. box-cutting or snakes) for file names, and Gem names: Consistent Naming. That article you cited for lowercasenounderscore. rb is old and I haven't seen anyone else claiming that as the convention. Underscores is almost universally the standard.

How do I find the class of an object in Ruby?

Use #is_a? to Determine the Instance's Class Name in Ruby If the object given is an instance of a class , it returns true ; otherwise, it returns false . #is_a also returns true if the object is an instance of any subclasses.


1 Answers

I think Artem Kalinchuk's last comment deserves to be the answer of this misworded question.

A Ruby class name can contain numbers.

A Rails class has to be defined in a correctly named file. If I define a class called NewYear2012Controller:

Correct file name: new_year2012_controller.rb
Incorrect file name: new_year_2012_controller.rb (note the extra underscore)

Because this is how Rails inflector and auto-loading works.

like image 95
lulalala Avatar answered Sep 21 '22 12:09

lulalala