Ruby中是否有创建一个元素的数组,是的同一类型?Ruby:单元素类型的数组
例:
class User
end
my_array = UserArray.new
我当然可以手动创建类,但我宁愿有一个默认的行为,因为我需要它为许多不同的类。
感谢
Ruby中是否有创建一个元素的数组,是的同一类型?Ruby:单元素类型的数组
例:
class User
end
my_array = UserArray.new
我当然可以手动创建类,但我宁愿有一个默认的行为,因为我需要它为许多不同的类。
感谢
你是什么意思是?简单地是?喜欢这个?
class User; end
my_array = 5.times.map { User.new }
或者必须?喜欢这个?
class << Array
def of klass_in_plural
require 'active_support/inflector' # gem install activesupport if necessary
klass = const_get klass_in_plural.to_s.singularize
Class.new self do
define_method :check do
require 'y_support/typing' # gem install y_support if necessary
aT_all_kind_of klass # runtime assertion that raises TypeError unless
end # all the elements of self are #kind_of? klass
class << self
def [] *args; super.check end
def new *args; super.check end
end
def << arg; super.check end
def + arg; self.class.new super end
def - arg; self.class.new super end
# etc etc
end
end
end
a = Array.of(:Integers)[ 1, 2, 3 ]
#=> [ 1, 2, 3 ]
b = Array.of(:Integers)[ 1, 2, 3.3 ]
#=> TypeError: Each collection element must be kind of Integer!
c = Array.of(:Hashes)[ { a: 42 } ]
#=> [{a: 42}]
d = Array.of(:Hashes)[ 42 ]
#=> TypeError: Each collection element must be kind of Hash!
e = Array.of(:Users)[ User.new, User.new ]
#=> [#<User:0xb7cd8040>, #<User:0xb7cdaa0c>]
@Jeffery,我的代码的第二部分有点棘手,我必须在发布之前对它进行测试。 –
是的,你称之为“第一部分”真的是一个评论。 – Shoe
'Array.new(3,“12”)'会创建一个3''12“'对象的数组,它们会将相同的对象保持不变?这不可能是答案,为什么更多的代码?或者可能是我没有得到OP的重点。 –
我会多加一个答案,一个不解决字面OP的问题,但解决了提问者可能有问题。 User
类型的主要类通常将其实例保存在某种民事登记中。也就是说,对象空间中存在一个或多个集合,其中包含用户名称和其他可能的ID,这些集合都可以用于唯一标识User
实例。然后,我们担心验证对象是否是用户或用户标识。这种情况经常遇到,至少对于命名部分,我写了一个宝石,y_support/name_magic
。这个gem的灵感来自于Ruby中的类和模块经常被命名,它们可以通过不断的赋值来命名,甚至还有内置的#name
方法返回它们的名字。与gem install y_support
安装name_magic
和按如下方式使用它:
require 'y_support/name_magic'
class User
include NameMagic
def to_s; "user #{name or object_id}" end
def inspect; "#<#{self}>" end
end
# we can now construct named users easily:
u = User.new name: "Cathy"
u.name #=> :Cathy
User::Cathy #=> #<user Cathy>
# and we also have constant magic at our disposal to construct named users:
Fred = User.new #=> #<user Fred>
# By including NameMagic, User class has acquired the registry of instances:
User.instances #=> [#<user Cathy>, #<user Fred>]
# And the ability to indifferently access those instances by their ids:
User.instance(u) #=> #<user Cathy>
User.instance(:Cathy) #=> #<user Cathy>
User.instance("Fred") #=> #<user Fred>
User.instance(:Augustin) #=> NameError: No instance Augustin in User.
# Anonymous users can be constructed, too:
u = User.new
# And named later
u.name = "Joe"
# We can notice that although user "Cathy" is no longer explicitly assigned
# to any variable (since u contains Joe now), it is still registered in the
# @instances instance variable owned by User class and serviced by NameMagic
# mixin. So Cathy continues to live as a User instance:
User.instances #=> [#<user Cathy>, #<user Fred>, #<user Joe>]
# If we wanted Cathy garbage collected, we would have to forget her explicitly
User.forget :Cathy # returns the unnamed user Cathy for the last time
User::Cathy #=> NameError: uninitialized constant User::Cathy
而在这一点上,我通常定义构造#User
这样我就不必键入“.new
”一遍又一遍:
def User *args, &block
User.new *args, &block
end
而且实例访问#user
,这样我就不必键入“User.instance
”一遍又一遍:
def user user
User.instance user
end
之后,我有能力处理的情况下识别和验证类的问题:
# Constructing new users:
User name: "Augustin" #=> #<user Augustin>
Quentin = User() #=> #<user Quentin>
#() is necessary to distinguish the method #User from the constant User
user :Quentin #=> #<user Quentin>
user :Arun #=> NameError: No instance Arun in User.
# I will subclass Array to define an array of users:
class Users < Array
class << self
def [] *args; super *args.map { |arg| user arg } end
def new arg; super arg.map { |e| user e } end
end
end
# And I will define conveninece constructors #Users and #users:
def Users arg; Users.new arg end
def users *args; Users[ *args ] end
# Now I have indifferent access regardless whether the elements are instances or
# instance ids (ie. names):
Users [ Fred, :Augustin ] #=> [#<user Fred>, #<user Augustin>]
# And I validate that the collection elements must be User instances or their ids:
users :Augustin, :Quentin #=> [#<user Augustin>, #<user Quentin>]
users :Augustin, :Arun # NameError: No instance Arun in User.
要完成的游,让我们回顾一下我们所创建的实例,并且注意到也name_magic
定义的方法Array#names
:
users = User.instances
#=> [#<user Fred>, #<user Joe>, #<user Augustin>, #<user Quentin>]
user_names = users.names
#=> [:Fred, :Joe, :Augustin, :Quentin]
你为什么需要这门课?为什么不'my_array = []','my_array << User.new'? – falsetru
@falsetru我想,他想限制数组使用特定的元素,如集合... –
如果你想要类似Java中的泛型,Ruby不支持这一点。你需要自己实现它。 – plu