• 如何将单一吨位延伸到某类的若干物体,即:我如何能够拥有最多不超过5个等级的物体。
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 ...
• 如何将单一吨位延伸到某类的若干物体,即:我如何能够拥有最多不超过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
由于标的到
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 ...
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 ...
All of the following API do the same thing: open a file and call a block for each line. Is there any preference we should use one than another? File.open("file").each_line {|line| puts line} open("...
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?
Here s the string: 04046955104021109 I need it to be formatted like so: 040469551-0402-1109 What s the shortest/most efficient way to do that with ruby?
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
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 ...
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 ...