Skip to content

Instantly share code, notes, and snippets.

@allolex
Created October 14, 2015 22:13
Show Gist options
  • Save allolex/c6d345d249de70efd8dc to your computer and use it in GitHub Desktop.
Save allolex/c6d345d249de70efd8dc to your computer and use it in GitHub Desktop.

Revisions

  1. allolex created this gist Oct 14, 2015.
    65 changes: 65 additions & 0 deletions class_oop.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,65 @@
    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