Created
April 4, 2015 21:54
-
-
Save phlipper/2fbad2dda59bfcb5581e to your computer and use it in GitHub Desktop.
TECH603 Day 4 - Working with Cups
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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