Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
class MyClass
@@my_class_variable = "Class variable content."
def initialize
@my_instance_variable = "Instance variable content."
end
# Class methods have a "self." before their name. For now we won't go into the details
# on why it is that way.
def self.thing_my_class_does
puts "Inside the instance method!"
puts @@my_class_variable
end
def thing_an_instance_of_my_class_does
puts "Inside the instance method!"
puts @my_instance_variable
end
end
MyClass.thing_my_class_does
# This will print the following to the screen:
# Inside the class method!
# Instance variable content.
MyClass.new.thing_my_class_does
# This will cause a NoMethodError, since the instance of MyClass does not have such method:
# NoMethodError: undefined method `thing_my_class_does' for #<MyClass:0x0055a389098170>
MyClass.new.thing_an_instance_of_my_class_does
# This will print the following to the screen:
# Inside the instance method!
# Instance variable content.
MyClass.thing_an_instance_of_my_class_does
# This will also cause a NoMethodError, since MyClass does not have such method:
# NoMethodError: undefined method `thing_an_instance_of_my_class_does' for MyClass:Class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.