Ruby class symbols # and :: and class methods
In Ruby classes and namespaces, I often see the # character between the class name and member name when talking about the implementation, and other times :: is used. The period . seems to be consistently used only for instantiated objects of the class (to send a message, like Smalltalk). I'm confused about what these different symbols are conveying?
Here's a breakdown of what each symbol conveys:
# (hash): This refers to instance methods of a class. When you see ClassName#method_name, it means the method_name is a method defined within the ClassName class and can be called on instances (objects) of that class.
:: (colon-colon): This is the scope resolution operator (our old friend). It's used to access:
-
Class constants:
ModuleName::CONSTANT_NAMErefers to a constant defined within theModuleNamemodule or class. -
Nested modules/classes:
OuterModule::InnerModule::ClassNameaccessesClassNamewhich is nested withinInnerModulewhich is further nested withinOuterModule.
. (period): This is used to call methods on instantiated objects. Once you create an object of a class (e.g., my_object = MyClass.new), you use the period to call its instance methods: my_object.method_name.
In summary
| Symbol | Usage | Example |
|---|---|---|
# |
Instance methods of a class | String#upcase |
:: |
Class constants or nested modules/classes |
Math::PI, Network::Connection::TCP
|
. |
Calling methods on instantiated objects |
my_string.reverse, my_object.say_hello
|
Ruby classes and modules can define both instance methods (called with #) and constants (accessed with ::). The period is reserved specifically for sending messages to objects you've created from a class (calling methods).
Ruby instance methods and class methods
In Java, there are instance methods, and static class methods. Ruby doesn't have static methods in the exact same way as Java. However, it achieves similar functionality with class methods.
Here's the difference:
- Java static methods: These are methods defined within a class but not tied to any specific instance. They are called directly on the class itself using the class name followed by a dot (.).
-
Ruby class methods: These are defined within a class using
def self.method_namesyntax. They operate on the class itself or manage aspects related to all instances of the class. You call them using the class name followed by::(colon-colon) and the method name.
Here's an example comparing static methods in Java and class methods in Ruby:
Java:
public class MathUtils {
public static int add(int a, int b) {
return a + b;
}
}
int result = MathUtils.add(5, 3); // Calling static method
Ruby:
class MathUtils
def self.add(a, b)
a + b
end
end
result = MathUtils::add(5, 3) # Calling class metho
Both achieve the same outcome - calling a method that doesn't require an object instance. However, Ruby's approach emphasizes that the method belongs to the class itself.
Key Points:
- Ruby uses
def self.method_namefor class methods. - You call them using
ClassName::method_name. - They are useful for utility functions or logic related to the class as a whole, rather than specific instances.
- The period (
.) is used only for sending messages to instantiated objects of a class.