When we call sub class instance foo via constant ruby searches for foo constant in myclass and its superclasses.
Ruby define constant in class.
Uses the instance method.
Module constants true constants of module and.
Note that constants do not exist until a value is actually assigned to them.
The class customer can be displayed as class customer end you terminate a class by using the keyword end.
Your confusion is due to the fact that the class method module constants hides the instance method module constants for module.
6 1 reloading and stale objects.
So this is the way ruby s constant lookup algorithm works.
It never looks in subclass and so it can t find a definition for the constant.
Same class method as in 1 8.
A method is a thing your class can do.
Module constants all constants one argument.
In ruby 1 9 this has been addressed by adding an optional parameter.
Ruby performs a lookup for the constant that follows a class or module keyword because it needs to know if the class or module is going to be created or reopened.
This will produce the following result.
The name should always be in initial capitals.
All the data members in the class are between the class definition and the end keyword.
You can squeeze an orange to get juice.
Usr bin ruby class example var1 100 var2 200 def show puts value of first constant is var1 puts value of second constant is var2 end end create objects object example new object show here var1 and var2 are constants.
Classes become more useful when you start adding instance methods instance variables to them.
As you can see the class object stored in the user constant is different after reloading.
So just define your constants outside methods typically we want to have constant definitions at the top of your class so they are clearly visible.
A class in ruby always starts with the keyword class followed by the name of the class.
Here s a code example.
Although constants should not be changed you can modify the internal states of the objects they reference as seen in.
Class orange def squeeze puts here s your juice end end orange orange new orange squeeze.
Ruby class and module names are also constants but they are conventionally written using initial capital letters and camel case likethis.
6 1 1 constants after the class and module keywords.