Created
March 10, 2014 22:35
-
-
Save carlossanchezp/9475926 to your computer and use it in GitHub Desktop.
Métodos de clase y métodos de instancia
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
1) Sin herencia métodos de clase y de instancia | |
=============================================== | |
class Foo | |
def self.some_class_method | |
puts self | |
end | |
def some_instance_method | |
puts self.class | |
self.class.some_class_method | |
end | |
end | |
NOTA para Class Method: | |
======================= | |
Foo.some_class_method | |
Nota para Instance Method: | |
========================== | |
Foo.new.some_instance_method | |
2) Con herencia métodos de clase y de instancia | |
================================================ | |
NOTA: usar self.class.method no es lo mismo que usar ClassName.method cuando hacemos herencia entre clases. | |
class Foo | |
def self.default_make | |
"Foo" | |
end | |
def foo_one | |
self.class.default_make | |
end | |
def foo_two | |
Foo.default_make | |
end | |
end | |
class BigFoo < Foo | |
def self.default_make | |
"BigFoo" | |
end | |
end | |
RESULTADO en consola: | |
==================== | |
b=BigFoo.new | |
b.foo_one => "BigFoo" | |
b.foo_two => "Foo" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment