Questions for beginners. Implementation of Ruby classes for example

advertisements

Before reading. I'm new in Ruby and I trying to learn by examples and exercises. I found some exercise, and I'm stuck on it.

Code a Ruby program that simulates a Hotel room lot. User will enter, issued a ticket, exit and of course pay.

Basic Required Scenarios:

User can enter hotel if there is enough rooms. User can exit, if it paid User that is already in the Hotel, can’t enter again before exiting. This what i did:

   class Hotel < User
      attr_accessor :room
      def initialize(room)
          @room = []
          super(name)
      end

      def on_entry
          if room.count < 20
          new_array = []
          rooms.each do |space|
              new_array << space
              print("You just rent a room!")
              end
          else
              print("Thank you for coming!")
          end
      end

      def on_exit
          if name.exist
              user.time_dindong
          else
              puts "Please, pay for room first"
          end
      end
    end

    class User
       attr_accessor :name, :id
       def initialize(name, id)
          @name = name
          @id = id
       end
    end

I getting error: Hotel.rb:1:in `': uninitialized constant User (NameError) Who can please told me what I doing wrong. If it have another way to do that?

Thanks for your time.


You need to declare User class before Hotel class

class User
   attr_accessor :name, :id
   def initialize(name, id)
      @name = name
      @id = id
   end
end

class Hotel < User
  # ...

And I don't think you need to extend Hotel from User. It must be:

class Hotel
  # ..