Last active
July 26, 2016 08:48
-
-
Save grittyninja/8dfbac870860065636943fd69ec9004f to your computer and use it in GitHub Desktop.
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
# Class methods are methods that are called on a class and instance methods are methods that are called on an instance of a class. | |
class Foo | |
# Class method | |
class << self | |
def bar | |
puts "class method" | |
end | |
end | |
# Instance method | |
def buzz | |
puts "instance method" | |
end | |
end | |
boo = Foo | |
boo.bar | |
zoom = Foo.new | |
zoom.buzz | |
# output: | |
# class method | |
# instance method | |
bu = Foo.new | |
bu.bar | |
# main.rb:27:in `<main>': undefined method `bar' for #<Foo:0x007efe9b97d538> (NoMethodError) | |
tem = Foo | |
tem.buzz | |
# main.rb:36:in `<main>': undefined method `buzz' for Foo:Class (NoMethodError) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment