Last active
July 26, 2023 14:28
-
-
Save ddeveloperr/a9759502faf6293dc9c6 to your computer and use it in GitHub Desktop.
ruby Colon access operator explained with example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Double Colon (::) is namespace resolution. It can also call Class Level Constants. So when you do this: | |
MyModule::MyClass::MySubClass | |
MyModule::MyClass::MySubClass::CONSTANT | |
# it is calling the name space of: | |
module MyModule | |
class MyClass | |
class MySubClass | |
CONSTANT = 'test' | |
end | |
end | |
end | |
# This differs from Dot Operators (.) in that with Dot you are calling a property or method. | |
# This will call a method | |
MyModule::MyClass::MySubClass.new | |
MyModule::MyClass::MySubClass.run | |
MyModule::MyClass::MySubClass.property = test | |
MyModule::MyClass::MySubClass.property #=> test | |
#This either initializes the object or calls a class method: | |
module MyModule | |
class MyClass | |
class MySubClass | |
CONSTANT = 'test' | |
attr_accessor :property | |
def initialize | |
# stuff | |
end | |
def property=(property) | |
@property = property | |
end | |
def property | |
@property | |
end | |
def self.run | |
# more stuff | |
end | |
end | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment