Created
October 14, 2015 22:13
-
-
Save allolex/c6d345d249de70efd8dc to your computer and use it in GitHub Desktop.
This file contains hidden or 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 Table | |
# attr_reader :num_legs | |
# attr_writer :num_legs | |
attr_accessor :num_legs | |
def initialize(legs) | |
@tabletop = [] | |
@num_legs = legs | |
end | |
def self.has_legs? | |
true | |
end | |
# replaced by attr_reader and attr_accessor | |
# def num_legs | |
# @num_legs | |
# end | |
# replaced by attr_writer and attr_accessor | |
# def num_legs=(value) | |
# @num_legs = value | |
# end | |
def put_on(something) | |
@tabletop << something | |
end | |
def inventory | |
@tabletop | |
end | |
def look_at | |
if @tabletop.size > 0 | |
puts "This table has the following items:" | |
@tabletop.each do |item| | |
puts "- #{item}" | |
end | |
else | |
puts "This table has no items." | |
end | |
end | |
end | |
p Table.has_legs? | |
__END__ | |
t = Table.new 4 | |
t.look_at | |
t.put_on "plate" | |
t.put_on "book" | |
t.look_at | |
puts t.num_legs | |
t.num_legs = 2 | |
puts t.num_legs | |
p t.inventory | |
# t2 = Table.new | |
# p t2.put_on 2 | |
# p t2.look_at | |
# | |
# p t.look_at |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment