English 中文(简体)
如何将某类物体的数量限制在废墟上?
原标题:How to restrict the number of objects of a class to a given number in ruby?

• 如何将单一吨位延伸到某类的若干物体,即:我如何能够拥有最多不超过5个等级的物体。

最佳回答

Multiton,因篇幅限制。

这里是你可以包含在任何类别中的多语种模块的疯狂实施。 它可以制造的物体为5个,你可以确定一个称为<条码>的类别方法,将一定数目(允许的最大数目)退还给你的类别。

module Multiton

  MAX_INSTANCES = 5
  COUNT_HOOK = :max_instances

  module MultitonClassMethods
    def instance
      size = @instances.size
      max = respond_to?(COUNT_HOOK) ? self.send(COUNT_HOOK) : MAX_INSTANCES
      @instances << new if size < max
      @instances[rand(size)]
    end
  end

  def self.included(klass)
    klass.class_eval {
      @instances = []
    }
    klass.private_class_method :new
    klass.extend(MultitonClassMethods)
  end

end

将模块列入一个类别,使其成为一个多州。

# Falls back to Multiton::MAX_INSTANCES 
class Person
  include Multiton
end

# Overrides the number of allowed instances in max_instances
class Resource
  include Multiton

  def self.max_instances
    58
  end
end

由于这些物体是随机从该多州的一个集合中退回的,你可能在短期内无法收回所有物体。 但是,由于要求更多的物体,它甚至应当停止。 您可在<代码>Multitonmodule中改变这一行为,通过物体放弃而不是任意取走。

people = []
1000.times do
  people << Person.instance
end
# should print 5, but may print a smaller number
p people.uniq.size

resources = []
1000.times do
  resources << Resource.instance
end
# should print 58, but may print a smaller number
p resources.uniq.size
问题回答

例:

# Example class which can be instanciated at most five times
# Naive approach with Class variable

class FiveAtMost
  @@instances = 0

  def initialize()
    if @@instances >= 5
      raise "No more than five instances allowed."
    else
      @@instances += 1
    end
    p "Initialized instance #{@@instances}"
  end
end

one = FiveAtMost.new
two = FiveAtMost.new
three = FiveAtMost.new
four = FiveAtMost.new
five = FiveAtMost.new

# will raise RuntimeError: No more than five instances allowed.
six = FiveAtMost.new

由于标的到garbage 收集<>/em>的时机不可预测,你需要为此开展某种工作。 或许大家认为这很有用:





相关问题
Ruby parser in Java

The project I m doing is written in Java and parsers source code files. (Java src up to now). Now I d like to enable parsing Ruby code as well. Therefore I am looking for a parser in Java that parses ...

rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

multiple ruby extension modules under one directory

Can sources for discrete ruby extension modules live in the same directory, controlled by the same extconf.rb script? Background: I ve a project with two extension modules, foo.so and bar.so which ...

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

热门标签