Classes & Objects

Classes

class Elf
attr_reader :name

def initialize(name)
@name = name
end

def use_magic
"#{@name} casts magic missile"
end
end

legolas = Elf.new('Legolas')
drizzet = Elf.new('Drizzet')
puts legolas.name
=> 'Legolas'
puts legolas.use_magic
=> 'Legolas casts magic missle'
drizzit.name
=> 'Drizzet'
drizzet.use_magic
=> 'Drizzet casts magic missle'
class Human
def self.species
'I am a Human'
end
end

bob = Human.new
bob.species
=> NoMethodError (…)
Human.species
=> 'I am an Human'
class Human
@@count = 0

def initialize
@@count += 1
end

def self.count
@@count
end
end

bob = Human.new
Human.count
=> 1
class Species
attr_reader :name

def initialize(name)
@name = name
end

def use_magic
"#{@name} casts magic missile"
end
end

class Human < Species
end

class Elf < Species
end

gandalf = Human.new('Gandalf')
gandalf.use_magic
=> 'Gandalf casts magic missile'
legolas = Elf.new('Legolas')
legolas.use_magic
=> 'Legolas casts magic missile'
module Magic
def use_magic
"#{@name} cast magic missile"
end
end

class Species
attr_reader :name

def initialize(name)
@name = name
end
end

class Human < Species
end

class Elf < Species
include Magic
end

class Dwarf < Species
include Magic
end
aragorn = Human.new('Aragorn')
aragorn.use_magic
=> NoMethodError (..)
arwen = Elf.new('Arwen')
arwen.use_magic
=> 'Arwen casts magic missile'
gimli = Dwarf.new('Gimli')
gimli.use_magic
=> 'Gimli cats magic missile"
module Bark
end
module Swim
end
module Birth
end
class Animal
include Birth
end
class Dog < Animal
include Swim
include Bark
end
Dog.ancestors
=> [Dog, Bark, Swim, Animal, Birth, Object, Kernel, BasicObject]

Objects

class Person  
def initialize(name)
@name = name
end

def name
@name
end
end

bob = Person.new('Bob')
bob.name
=> 'Bob' ​
class Person
def initialize(name)
@name = name
end
# getter
def name
@name
end
# setter
def name=(new_name)
@name = new_name
end
end
class Person
attr_accessor :name
attr_reader :age
attr_writer :weight
def initialize(name)
@name = name
@age = 20
@weight = 0
end
def get_weight
@weight
end
end
bob = Person.new('Bob')
bob.name
=> 'Bob'
bob.name = 'Rick'
bob.name
=> 'Rick'
bob.age
=> 20
bob.weight
=> NoMethodError (...)
bob.weight = 200
bob.get_weight
=> 200
class Wizard
def use_magic
"Magic missile was cast"
end
end
class Person
attr_accessor :name, :role
def initialize(name)
@name = name
end
end

bob = Person.new('Bob')
wizard = Wizard.new
bob.role = wizard
bob.role.use_magic
=> 'Magic missile was cast'
class Wizard
end
class Person
attr_accessor :name
def initialize(name)
@name = name
end
def to_s
"My name is #{name} and I am not a wizard"
end
end
gandalf = Wizard.new
puts gandalf
=> #<Wizard:0x0000000001d23a38>
bob = Person.new('Bob')
puts bob
=> 'My name is Bob and I am not a wizard'

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store