Skip to content

Instantly share code, notes, and snippets.

@grittyninja
Last active July 26, 2016 08:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grittyninja/8dfbac870860065636943fd69ec9004f to your computer and use it in GitHub Desktop.
Save grittyninja/8dfbac870860065636943fd69ec9004f to your computer and use it in GitHub Desktop.
# 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