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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With