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
| const db = [ | |
| { | |
| name: 'John Doe', | |
| age: 28, | |
| lastModified: Date.now(), | |
| lastAccessed: Date.now() | |
| }, | |
| { | |
| name: 'Jane Smith', | |
| age: 30, |
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
| instance_variable_set(name, value) # Nos permite definir y asignar un valor a una nueva variable de instancia | |
| define_method(name, &block) # Nos permite definir dentro de la clase un método de manera dinámica y en tiempo de ejecución | |
| method_missing(name, *args, &block) # Nos permite capturar y manejar la excepción que se dispara cuando llamamos un método no definido dentro de una clase. | |
| ... |
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
| class MethodCatcher | |
| def method_missing(name, *args, &block) | |
| puts "El nombre del método no encontrado es #{name}" | |
| puts "los argumentos del método son #{args}" | |
| puts "El cuerpo del método es #{block.inspect}" | |
| end | |
| end | |
| catch = MethodCatcher.new | |
| catch.some_method(1, 2) { puts "something" } |
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
| class Example | |
| end | |
| ex = Example.new | |
| ex.instance_variables # => [] | |
| ex.instance_variable_set(:@x, 1) # => 1 | |
| ex.instance_variables # => ["@x"] | |
| ex.instance_variable_defined?(:@x) # => true | |
| ex.instance_variable_defined?(:@y) # => false | |
| ex.instance_variable_get(:@x) # => 1 |
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
| # Esta definición: | |
| class Example | |
| attr_accessor :x | |
| end | |
| ## Es equivalente a: | |
| class Example | |
| def x | |
| @x | |
| end |
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
| Class.methods.select { |m| m =~ /attr/ } # [:attr, :attr_reader, :attr_writer, :attr_accessor] |
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
| # Clase base con la definición de nuestra macro a nivel de clase: | |
| class AttrCustom | |
| def self.attr_custom(name) | |
| define_method("#{name}=") do |value| | |
| puts "Asignando #{value.inspect} a #{name}" | |
| instance_variable_set("@#{name}", value) | |
| end | |
| define_method("#{name}") do | |
| puts "Leyendo #{name}" |
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
| 42.class # => Fixnum | |
| :foo.class # => Symbol | |
| "Ruby".class # => String | |
| true.class # => TrueClass | |
| [1, 2, 3].class # => Array | |
| { a: => 1 }.class # => Hash |
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
| 42 + 1 # => 43 | |
| 42.+(1) # => 43 | |
| 42.send(:+, 1) # => 43 |
NewerOlder