Skip to content

Instantly share code, notes, and snippets.

@mgiagante
Last active May 4, 2018 03:20
Show Gist options
  • Save mgiagante/60fe34d92dfec5b71032d54b955f54b5 to your computer and use it in GitHub Desktop.
Save mgiagante/60fe34d92dfec5b71032d54b955f54b5 to your computer and use it in GitHub Desktop.
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