Skip to content

Instantly share code, notes, and snippets.

@phlipper
Created April 4, 2015 21:54
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/2fbad2dda59bfcb5581e to your computer and use it in GitHub Desktop.
Save phlipper/2fbad2dda59bfcb5581e to your computer and use it in GitHub Desktop.
TECH603 Day 4 - Working with Cups
# define a class
class Cup
# class definition
def initialize
puts "I am initializing ... I should be a new cup"
@contents = "coffee"
end
def contents
@contents
end
def lid?
true
end
def empty
@contents = nil
puts "The cup should be empty"
end
end
# instance 1
cup = Cup.new
puts cup
# puts cup.object_id
puts cup.contents.inspect
puts cup.lid?
puts cup.empty
puts cup.contents.inspect
#instance 2
puts "====="
cup2 = Cup.new
puts cup2
# puts cup2.object_id
puts cup2.contents.inspect
puts cup2.lid?
puts cup2.empty
puts cup2.contents.inspect
# puts "".class
# puts Hash.new.class
# puts [].class
# puts 5.class
# puts 5.4.class
# puts :hi.class
# puts Cup.new.class
# puts Cup.class
class Cup
def initialize(contents = "")
# instance variables - `ivar`s
@contents = contents
@color = "blue"
@lid = false
end
attr_reader :color
attr_writer :color
attr_accessor :lid
def contents
@contents
end
def contents=(contents)
@contents = contents
end
def to_s
"[CUP] contents: #{contents} - color: #{color} - lid: #{lid}"
end
end
cup = Cup.new("wine")
puts cup
puts cup.contents
cup.contents = "juice"
puts cup.contents
puts cup.color
cup.color = "red"
puts cup.color
puts cup.lid
cup.lid = true
puts cup.lid
# define a class
class Cup
# class definition
def initialize(contents = "")
@contents = contents
@lid = false
@color = "green"
end
attr_accessor :contents
attr_accessor :lid
attr_reader :color
attr_writer :color
def lid?
@lid
end
def fill(drink)
self.contents = drink
end
def empty
self.contents = ""
end
end
# instance 1
cup = Cup.new("juice")
# cup.fill("juice")
puts "contents: #{cup.contents.inspect}"
puts "color: #{cup.color}"
puts "lid? #{cup.lid?}"
cup.lid = true
puts "lid? #{cup.lid}"
cup.color = "blue"
puts "color: #{cup.color}"
cup.contents = "something"
puts "contents: #{cup.contents.inspect}"
cup.empty
puts "contents: #{cup.contents.inspect}"
puts "=====" * 3
# instance 2
cup2 = Cup.new
cup2.fill("coffee")
puts "contents: #{cup2.contents.inspect}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment