将实例变量初始化为数组-Ruby [英] Initializing Instance Variable as an Array - Ruby
本文介绍了将实例变量初始化为数组-Ruby的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试将实例变量和实例变量初始化为数组,如下所示:
I'm trying to initialize and instance variable as an array as follows:
class Arch < ActiveRecord::Base
attr_accessor :name1
def initialize
@name1 = []
end
def add_name1(t)
@name1 << t
end
end
当我在控制台会话中尝试Arch.new时,我得到了(对象不支持#inspect).这是怎么回事?我如何使实例数组变量?我尝试遵循此,如下所示:
When I try Arch.new in a console session I get (Object doesn't support #inspect). What's up? How do I make an instance array variable? I tried to follow this like so:
class Arch < ActiveRecord::Base
attr_accessor :name1
def after_initialize
@name1 = []
end
def add_name1(t)
@name1 << t
end
end
,我的@ name1仍然是NilClass. :/
and my @name1 was still a NilClass. :/
推荐答案
您正在重写ActiveRecord的initialize
方法.尝试使用super
:
You are overriding ActiveRecord's initialize
method. Try using super
:
def initialize(*args, &block)
super
@name1 = []
end
这篇关于将实例变量初始化为数组-Ruby的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文