Skip to content

Instantly share code, notes, and snippets.

@phlipper
Created April 9, 2015 22:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phlipper/4a820d4b17a25ef15b6f to your computer and use it in GitHub Desktop.
Save phlipper/4a820d4b17a25ef15b6f to your computer and use it in GitHub Desktop.
TECH603 Day 5 example code - Working with Classes, Containers, and Cups
# shared `label_name` functionality
module Label
def label_name
"[#{self.class.to_s.upcase}] - Has a Label Name"
end
end
# Car - includes `Label`
class Car
include Label
attr_reader :name
def initialize
@name = "A Car"
end
end
# Container - our base for working with things to drink from - includes `Label`
class Container
include Label
attr_accessor :color,
:contents,
:handle,
:lid
def initialize(contents = :empty)
@color = ""
@contents = contents
@handle = false
@lid = false
end
def contents?
contents != :empty
end
def empty!
self.contents = :empty
end
alias_method :empty_contents, :empty!
def handle?
!!handle
end
def lid?
!!lid
end
def to_s
"[#{self.class.to_s.upcase}] contents: #{contents}, color: #{color}, handle: #{handle?}, lid: #{lid?}"
end
end
# Cup - inherits from Container
class Cup < Container
# default to blue
def initialize(contents = :empty)
super
@color = "blue"
end
end
# Mug - inherits from Container
class Mug < Container
# default white
# has a handle
def initialize(contents = :empty)
super
@color = "white"
@handle = true
end
end
# TravelMug - inherits from Container
class TravelMug < Container
# default to black
# no handle
# has a lid
def initialize(contents = :empty)
super
@color = "black"
@lid = true
end
end
car = Car.new
puts car
puts car.label_name
container = Container.new
puts container
puts container.label_name
# puts container.inspect
# puts container.class.ancestors.to_s
# puts container.methods.to_s
cup = Cup.new("juice")
cup.lid = "foo"
puts cup
# puts cup.inspect
# puts cup.class.ancestors.to_s
puts cup.label_name
# [CUP] contents: juice, color: blue, handle: false, lid: false
# #<Cup:0x007f13f7fc6d60 @color="blue", @contents="juice", @handle=false, @lid=false>
mug = Mug.new("coffee")
puts mug
puts mug.inspect
puts mug.class
# [MUG] contents: coffee, color: white, handle: true, lid: false
# #<Mug:0x007f13f7fc6ab8 @color="white", @contents="coffee", @handle=true, @lid=false>
tmug = TravelMug.new("tea")
puts tmug
puts tmug.inspect
puts tmug.class
# [TRAVELMUG] contents: tea, color: black, handle: false, lid: true
# #<TravelMug:0x007f13f7fc6888 @color="black", @contents="tea", @handle=false, @lid=true>
containers = [cup, mug, tmug]
containers.each do |container|
puts "Container [#{container.class}] has handle? #{container.handle?}"
end
class Cup
attr_accessor :color,
:contents,
:lid
def initialize(contents = :empty)
@color = "blue"
@contents = contents
@lid = false
end
def me
self
end
def contents?
contents != :empty
end
def empty!
self.contents = :empty
end
alias_method :empty_contents, :empty!
def fill_with(contents)
self.contents = contents
end
def lid?
lid
end
def to_s
"[CUP] contents: #{contents}, color: #{color}, lid: #{lid}"
end
end
cup = Cup.new("juice")
puts cup
puts cup.inspect
puts cup.contents?
puts cup.empty!
puts cup.empty_contents
cup.fill_with "water"
puts cup.color
puts cup
puts cup.me
puts cup.object_id == cup.me.object_id
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment