def welcome
puts "Welcome to my Lightningtalk!"
author = "Grecia Delgado"
theme = "Useful built-in methods of Object class in Ruby"
end
print welcome
¿Qué es una Clase Objecto en Ruby ?
Object.class
# Class
Array.class
# Class
Numeric.class
# Class
Class.class
# Class
Object.ancestors
# [Object, Kernel, BasicObject]
Array.ancestors
# [Array, Enumerable, Object, Kernel, BasicObject]
Numeric.ancestors
#[Numeric, Comparable, Object, Kernel, BasicObject]
Class.ancestors
#[Class, Module, Object, Kernel, BasicObject]
Kernel.class
# Module
BasicObject.class
#Class
Object.instance_methods
=> [:dup, :itself, :yield_self, :then, :taint, :tainted?, :untaint, :untrust,
:untrusted?, :trust, :frozen?, :methods, :singleton_methods, :protected_methods,
:private_methods, :public_methods, :instance_variables, :instance_variable_get,
:instance_variable_set, :instance_variable_defined?, :remove_instance_variable, :instance_of?,
:kind_of?, :is_a?, :tap, :class, :singleton_class, :display, :clone, :hash, :public_send, :method,
:public_method, :singleton_method, :define_singleton_method, :extend, :to_enum, :enum_for,
:<=>, :===, :=~, :!~, :nil?, :eql?, :respond_to?, :freeze, :inspect, :object_id, :send, :to_s, :__send__,
:!, :==, :!=, :equal?, :__id__, :instance_eval, :instance_exec]
Kernel.instance_methods
=>[:dup, :itself, :yield_self, :then, :taint, :tainted?, :untaint, :untrust, :untrusted?, :trust,
:frozen?, :methods, :singleton_methods, :protected_methods, :private_methods, :public_methods,
:instance_variables, :instance_variable_get, :instance_variable_set, :instance_variable_defined?,
:remove_instance_variable, :instance_of?, :kind_of?, :is_a?, :tap, :class, :singleton_class, :display,
:clone, :hash, :public_send, :method, :public_method, :singleton_method, :define_singleton_method,
:extend, :to_enum, :enum_for, :<=>, :===, :=~, :!~, :nil?, :eql?, :respond_to?, :freeze, :inspect,
:object_id, :send, :to_s]
¿Que métodos existen?
.instance_of
.is_a?
.send
.extended
.clone
.remove_instance_variable
.instance_of?
# el metodo retorna true or False
# Retorna true si el Obj es una instacia la clase dada
class Mascotas
end
class Gato < Mascotas
end
class GatoSilvestre < Gato
end
mishi = Gato.new
mishi.instance_of? Mascotas #=> false
mishi.instance_of? Gato #=> true
mishi.instance_of? GatoSilvestre #=> false
.is_a?
# el metodo retorna true or False
# Retorna true si Obj && instancia comparten clase||módulo
module Mamifero
end
class Mascotas
include Mamifero
end
class Gato < Mascotas
end
class GatoSilvestre < Gato
end
mishi = Mascotas.new
mishi.is_a? Mascotas #=> true
mishi.is_a? Gato #=> false
mishi.is_a? GatoSilvestre #=> false
mishi.is_a? Mamifero #=> true
.send
# Invoca el método identificado a través de un símbolo,
# pasándole los argumentos especificados
class MathOperations
def expo(num1, num2)
num1**num2
end
def sum (num1, num2, num3)
num1 + num2 + num3
end
end
expo = MathOperations.new
sum = MathOperations.new
expo.send :expo, 2,3 #retorna 8
sum.send :sum,5,5,6 #retorna 16
.extend
# Agrega a los obj los métodos de instancia
# de cada módulo dado como parámetro.
module Universo
def say_hello
"Hola desde el universo"
end
end
class Planeta
def message
"Hola desde el planeta"
end
end
saludo = Planeta.new
saludo.message #=> "Hola desde el planeta"
saludo.extend(say_hello) #<Planeta:0x0000558f8b8f17e8>
saludo.say_hello #=>"Hola desde el universo"
.clone
# el .clone clona el Obj junto con
# todas variables de instancia
class Cities
attr_accessor :food
def traffic
"it's chaotic!"
end
end
city1 = Cities.new
city1.food = "ceviche"
city1.inspect #<Cities:0x0000558f8b8135d8 @food=\"ceviche\">
city1.traffic #"it's chaotic!"
city2 = city1.clone
city2.inspect #"#<Cities:0x0000558f8b845768 @food=\"ceviche\">"
city2.traffic #"it's chaotic!"
.remove_instace_variable
remove_instance_variable
remove_instance_variable
# Elimina la variable de instancia nombrada de obj,
# devolviendo el valor de esa variable.
class Persons
attr_reader :age, :heigth , :job
def initialize
@job = "carpintero"
end
def remove
remove_instance_variable(:@job)
end
end
person = Persons.new
person.job #=> "carpintero"
person.remove #=> "carpintero"
person.job #=> nil
Gracias!
realizado por: Grecia Delgado
Methods Object class Ruby
By Grecia Delgado
Methods Object class Ruby
- 78