Objetives
# Syntaxes
class Person
# Class magic here ...
end
Person.new# Initialize
class Person
def initialize(name)
puts name
end
end
Person.new("Paulo")# Intance Variables
class Person
def initialize(name)
@name = name
end
end
person = Person.new("Paulo")# Instance Methds & Class Methods
class Person
def initialize(name)
@name = name
end
# Metodos de instancia
def say_hello
puts "Hi! My name is #{@name}"
end
# Metodos de clase
def self.type
p "I'm Happy Person!!"
end
end
Person.type
person = Person.new("Paulo")
person.current_name# Scope Variables
@name # Variable de instancia
@@type # Variable de clase
$title # Variable global# Scope Example
class Person
$title = "Fragmentado"
@@personalities = 0
def initialize(name)
@name = name
@@personalities += 1
end
def say_hello
"Hi! My name is #{@name}"
end
def self.current_type
@@personalities
end
end
# Make a new Person instance:
patricia = Person.new("Patricia")
# kevin = Person.new("kevin")
# dennis = Person.new("Dennis")
# barry = Person.new("Barry")
# the_beast = Person.new("The Beast")
puts "Current person greets: #{kevin.say_hello}"
# @name pertenece a la instancia person.
puts "Movie: #{$title}"
# $title es Global! Lo podemos obtener directamente.
puts "Type: #{Person.current_type}"
# @@type Pertenece a la clase Persona.# Syntaxes
class DerivedClass < BaseClass
# Some stuff!
end# Class inheritance
class ApplicationError
def display_error
puts "Error! Error!"
end
def hello
puts 'hello'
end
end
class SuperBadError < ApplicationError
end
err = SuperBadError.new
err.display_error
err.hello# Class inheritence - with initialize
class Application
def initialize(name)
@name = name
end
def say_name
@name
end
end
class MyApp < Application
end
application = Application.new("codeable.la")
my_app = MyApp.new("classroom.codeable.la")
p my_app.say_name# Override methods
class Eevee
def initialize(name = "eevee")
@name = name
end
def fight
"tackle"
end
end
class Vaporeon < Eevee
def fight
"Water gun"
end
end
vaporeon = Vaporeon.new('vaporeon')
p vaporeon# Just one!
class Eevee
def initialize(name)
@name = name
end
end
class Vaporeon
def initialize(name)
@name = name
end
end
# /////
class Ditto < Eevee; end
class Ditto < Vaporeon; end
# /////# Keyword super
class Eevee
def initialize(name = "eevee")
@name = name
@type = "normal"
end
def fight
"tackle"
end
end
class Vaporeon < Eevee
def initialize(name = "vaporeon")
# /////
super(name)
# /////
@type = "water"
end
def fight
"Water gun"
end
end
vaporeon = Vaporeon.new()
p vaporeonclass Message
@@messages_sent = 0
def initialize(from, to)
@from = from
@to = to
@@messages_sent += 1
end
end
class Email < Message
def initialize(from, to, subject)
super(from, to)
@subject = subject
end
end
my_message = Message.new("pato",'leti')
my_email = Email.new("perro","dogi", "Hi!!")
p my_email# Attribute readers & Attribute Writers
class Person
attr_reader :name
attr_writer :name
def initialize(name, job)
@name = name
@job = job
end
end# Attribute Accessor
class Person
attr_accessor :name
def initialize(name, job)
@name = name
@job = job
end
end