Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# define class Dog
class Dog
attr_accessor :breed, :name
def initialize(breed, name)
# Instance variables
@breed = breed
@name = name
end
def bark
puts 'Ruff! Ruff!'
end
def display
puts "I am of #{@breed} breed and my name is #{@name}"
end
# def name
# @name
# end
# def breed
# @breed
# end
# def name=(local_variable)
# @name = local_variable
# end
# def breed=(local_variable)
# @breed = local_variable
# end
end
# make an object
# Objects are created on the heap
# d = Dog.new('Labrador', 'Benzy')
my_shiny_new_dog = Dog.new('Labrador', 'Benzy')
=begin
Every object is "born" with certain innate abilities.
To see a list of innate methods, you can call the methods
method (and throw in a sort operation, to make it
easier to browse visually). Remove the comment and execute.
=end
# puts d.methods.sort
# Amongst these many methods, the methods object_id and respond_to? are important.
# Every object in Ruby has a unique id number associated with it
puts "The id of d is #{d.object_id}."
# To know whether the object knows how to handle the message you want
# to send it, by using the respond_to? method.
if d.respond_to?("talk")
d.talk
else
puts "Sorry, d doesn't understand the 'talk' message."
end
d.bark
d.display
# making d and d1 point to the same object
d1 = d
d1.display
# making d a nil reference, meaning it does not refer to anything
d = nil
d.display
# If I now say
d1 = nil
# then the Dog object is abandoned and eligible for Garbage Collection (GC)
class Cat
attr_accessor :breed, :name
def initialize(breed, name)
# Instance variables
@breed = breed
@name = name
end
def meow
puts 'Meow! Meow!'
end
def display
puts "I am of #{@breed} breed and my name is #{@name}"
end
end
my_newer_and_even_shinier_cat = Cat.new('Persian', 'Kitty')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.