Skip to content

Instantly share code, notes, and snippets.

@karthiks
Created October 22, 2011 07:46
Show Gist options
  • Save karthiks/1305753 to your computer and use it in GitHub Desktop.
Save karthiks/1305753 to your computer and use it in GitHub Desktop.
Ruby Quiz - object calling class method
#! ruby class_method.rb
class Test
def self.my_freakin_class_method
puts "In class method"
end
def my_freakin_instance_method
puts "In instance method"
end
end
t = Test.new
t.my_freakin_instance_method
Test.my_freakin_class_method # is the right and only way you call the class method. Receiver has to be of type Class.
t.my_freakin_class_method
#Output:
#[mp]$ ruby class_method.rb
#In instance method
#In class method
#class_method.rb:17:in `<main>': undefined method `my_freakin_class_method' for #<Test:0x9419ffc> (NoMethodError)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment