Created
October 22, 2011 07:46
-
-
Save karthiks/1305753 to your computer and use it in GitHub Desktop.
Ruby Quiz - object calling class method
This file contains hidden or 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
#! 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