Skip to content

Instantly share code, notes, and snippets.

@allolex
Created October 14, 2015 22:13
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 allolex/c6d345d249de70efd8dc to your computer and use it in GitHub Desktop.
Save allolex/c6d345d249de70efd8dc to your computer and use it in GitHub Desktop.
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