Los siguientes métodos sirven para determinar que módulos han sido incluídos y cuales son los ancestros de una clase o módulo:
[~/chapter8ReflectionandMetaprogramming]$ cat ancestryAndModules.rb module A; end module B; include A; end class C; include B; end if __FILE__ == $0 puts C < B # => true: C includes B puts B < A # => true: B includes A puts C < A # => true puts Fixnum < Integer # => true: all fixnums are integers puts Integer <Comparable # => true: integers are comparable puts Integer < Fixnum # => false: not all integers are fixnums puts (String < Numeric).inspect # => nil: strings are not numbers puts A.ancestors.inspect # => [A] puts B.ancestors.inspect # => [B, A] puts C.ancestors.inspect # => [C, B, A, Object, Kernel, BasicObject] puts String.ancestors.inspect # => [String, Comparable, Object, Kernel, BasicObject] puts C.include?(B) # => true puts C.include?(A) # => true puts B.include?(A) # => true puts A.include?(A) # => false puts A.include?(B) # => false puts A.included_modules.inspect # => [] puts B.included_modules.inspect # => [A] puts C.included_modules.inspect # => [B, A, Kernel] end
include
es un método privado de Module.
ruby-1.9.2-head :002 > Module.private_methods.select { |x| x=~ /include/ } => [:included, :include](Para mas información sobre
include
, véase la sección 14.7.2)
[~/chapter8ReflectionandMetaprogramming]$ cat -n including_class_methods1.rb 1 module Tutu 2 def self.tutu; "tutu"; end 3 end 4 5 class Chazam 6 include Tutu 7 end 8 9 puts Chazam.ancestors.inspect # [Chazam, Tutu, Object, Kernel, BasicObject] 10 11 chazam_eigenclass = class << Chazam; self; end 12 13 puts chazam_eigenclass.ancestors.inspect # [Class, Module, Object, Kernel, BasicObject] 14 15 Chazam.tutu
chazam_eigenclass = class << Chazam; self; end
obtiene una referencia al objeto que representa la singleton class, eigenclass o metaclass
de Chazam
.
Es posible obtener mas directamente la eigenclass de una clase
usando el método singleton_class
de Kernel:
ruby-1.9.2-head :001 > class Tutu; end => nil ruby-1.9.2-head :002 > Tutu.singleton_class => #<Class:Tutu>
Si tiene dudas para responder al ejercicio, repase las secciones Búsqueda de Métodos de Clase 14.14 y La Búsqueda por Métodos 14.13.
Tutu
?
¿Cual es el resultado de la llamada de la línea 17 en el siguiente código?
[~/chapter8ReflectionandMetaprogramming]$ cat -n including_class_methods2.rb 1 module Tutu 2 def tutu; puts "tutu"; end 3 end 4 5 class Chazam 6 class << self 7 include Tutu 8 end 9 end 10 11 puts Chazam.ancestors.inspect # [Chazam, Tutu, Object, Kernel, BasicObject] 12 13 chazam_eigenclass = class << Chazam; self; end 14 15 puts chazam_eigenclass.ancestors.inspect # [ Tutu, Class, Module, Object, Kernel, BasicObject] 16 17 Chazam.tutu
module Greeter; def hi; "hello"; end; end # A silly module s = "string object" s.extend(Greeter) # Add hi as a singleton method to s s.hi # => "hello"
String.extend(Greeter) # Add hi as a class method of String String.hi # => "hello"
module M class C Module.nesting # => [M::C, M] end end
Casiano Rodriguez León 2015-01-07