• 我在学习《R 和 RUBY 数据分析之旅第 3 章》,代码如下:

    class Restroom
      attr_reader :queue
      attr_reader :facilities
    
      def initialize(facilities_per_restroom=3)
        @queue=[]
        @facilities=[]
        @facilities_per_restroom.times{@facilities<<Facility.new}
      end
    
      def enter(person)
        [email protected]{|facility| not facility.occupied?}
        if unoccupied_facility
          unoccupied_facility.occupy person
        else
          @queue<<person
        end
      end
    
      def tick
        @facilities.each{|f| f.tick}
      end
    end 
    # class Restroom
    
    
    class Facility
      def initialize
        @occupier = nil
        @duration = 0
      end
      def occupy(person)
        unless occupied?
          @occupier = person
          @duration =1
          Person.population.delete person
          true
        else
          false
        end
      end
    
      def occupied?
        not @occupier.nil?
      end
    
      def vacate
        Person.population<<@occupier
        @occupier = nil    
      end
    
      def tick
        if occupied? and @duration > @occupier.use_duration
          vacate
          @duration = 0
        elseif occupied?
          @duration +=1
        end
      end
    end
    # class Facility
    
    class Person
      @@population = []
      attr_reader :use_duration
      attr_accessor :frequency
    
      def initialize(frequency=4,use_duration=1)
        @frequency= frequency
        @use_duration=use_duration
      end
    
      def self.population
        @@population
      end
    
      def need_to_go?
        rand(DURATION)+1<=@frequenty
      end
    end
    # class Person
    
    require 'csv'
    frequency=3
    facilities_per_restroom=3
    use_duration=1
    population_range=10..600
    
    data={}
    population_range.step(10).each do |population_size|
      Person.population.clear
      population_size.times{Person.population<<Person.new(frequency,use_duration)}
      data[population_size]=[]
      restroom=Restroom.new facilities_per_restroom
      DURATION.times do |t|
        data[population_size]<<restroom.queue.size
        queue=restroom.queue.clone
        restroom.queue.clear
        unless queue.empty
          restroom.enter queue.shift
        end
        Person.population.each do |person|
          if person.need_to_do
            restroom.enter person
          end
        end
        restroom.tick
      end
    end
    
    CSV.open('simulation1.csv','w') do |csv|
      lbl=[]
      population_range.step(10).each{|population_size|lbl<<population_size}
      csv<<lbl
      DURATION.time do |t|
        row =[]
        population_range.step(10).each do |population_size|
          row <<data[population_size][t]
        end
        csv <<row
      end
    
    end
    

    运行结果如下:

    restroom.rb:90:in `step': no block given (LocalJumpError)
    from restroom.rb:90
    
    
    第90行:population_range.step(10).each do |population_size|
    

    哪里错了,请指点!

  • 我在学习《R 和 RUBY 数据分析之旅第 3 章》,代码如下: class Restroom attr_reader :queue attr_reader :facilities

    def initialize(facilities_per_restroom=3) @queue=[] @facilities=[] @facilities_per_restroom.times{@facilities<<Facility.new} end

    def enter(person) unoccupied_facility=@facilities.find{|facility| not facility.occupied?} if unoccupied_facility unoccupied_facility.occupy person else @queue<<person end end

    def tick @facilities.each{|f| f.tick} end end

    class Restroom

    class Facility def initialize @occupier = nil @duration = 0 end def occupy(person) unless occupied? @occupier = person @duration =1 Person.population.delete person true else false end end

    def occupied? not @occupier.nil? end

    def vacate Person.population<<@occupier @occupier = nil
    end

    def tick if occupied? and @duration > @occupier.use_duration vacate @duration = 0 elseif occupied? @duration +=1 end end end

    class Facility

    class Person @@population = [] attr_reader :use_duration attr_accessor :frequency

    def initialize(frequency=4,use_duration=1) @frequency= frequency @use_duration=use_duration end

    def self.population @@population end

    def need_to_go? rand(DURATION)+1<=@frequenty end end

    class Person

    require 'csv' frequency=3 facilities_per_restroom=3 use_duration=1 population_range=10..600

    data={} population_range.step(10).each do |population_size| Person.population.clear population_size.times{Person.population<<Person.new(frequency,use_duration)} data[population_size]=[] restroom=Restroom.new facilities_per_restroom DURATION.times do |t| data[population_size]<<restroom.queue.size queue=restroom.queue.clone restroom.queue.clear unless queue.empty restroom.enter queue.shift end Person.population.each do |person| if person.need_to_do restroom.enter person end end restroom.tick end end

    CSV.open('simulation1.csv','w') do |csv| lbl=[] population_range.step(10).each{|population_size|lbl<<population_size} csv<<lbl DURATION.time do |t| row =[] population_range.step(10).each do |population_size| row <<data[population_size][t] end csv <<row end

    end

    运行结果如下: restroom.rb:90:in `step': no block given (LocalJumpError) from restroom.rb:90

    第 90 行:population_range.step(10).each do |population_size|

    哪里错了,请指点!