我是Ruby的新手,所以请原谅可能的noob问题,但Google迄今为止一直没有帮助。修改子类的常量
我在我的父类
Items = [[1, 3, 5], [2, 4, 6]]
定义的类恒定然后我有一个子类,其中我要添加的阵列[1,5,9]到项目阵列。你能以这种方式引用super /重新定义常量吗?
喜欢的东西
Items = super.Items.concat([1, 5, 9])
我想不需要在项目定义复制到我的子类与附加项目。
我是Ruby的新手,所以请原谅可能的noob问题,但Google迄今为止一直没有帮助。修改子类的常量
我在我的父类
Items = [[1, 3, 5], [2, 4, 6]]
定义的类恒定然后我有一个子类,其中我要添加的阵列[1,5,9]到项目阵列。你能以这种方式引用super /重新定义常量吗?
喜欢的东西
Items = super.Items.concat([1, 5, 9])
我想不需要在项目定义复制到我的子类与附加项目。
常量名称类中间隔您可以通过访问superclass
建立在它或者它们被定义的模块。他们通过通常的祖先路径解决。在你的子类中,你可以定义一个与超类中的名称相同的名称的常量,并且初始化它的表达式可以引用超类的常量,因为直到初始赋值后子类的常量才会被定义。就像这样:
$ pry
[1] pry(main)> class A; Items = [[1, 3, 5], [2, 4, 6]]; end
=> [[1, 3, 5], [2, 4, 6]]
[2] pry(main)> class B < A; end
=> nil
[3] pry(main)> class B; Items; end
=> [[1, 3, 5], [2, 4, 6]]
[4] pry(main)> A::Items
=> [[1, 3, 5], [2, 4, 6]]
[5] pry(main)> B::Items
=> [[1, 3, 5], [2, 4, 6]]
[6] pry(main)> class B; Items = Items.dup << [7,8,9]; end
=> [[1, 3, 5], [2, 4, 6], [7, 8, 9]]
[7] pry(main)> A::Items
=> [[1, 3, 5], [2, 4, 6]]
[8] pry(main)> B::Items
=> [[1, 3, 5], [2, 4, 6], [7, 8, 9]]
当导出新常量,如果你打算使用不同诱变方法(如Array#<<
)来修改它要小心dup
原件。见陷阱:
[9] pry(main)> class A; Foo = [[1,2],[3,4]]; end
=> [[1, 2], [3, 4]]
[10] pry(main)> A::Foo
=> [[1, 2], [3, 4]]
[11] pry(main)> class B; Foo = Foo << [5,6]; end
=> [[1, 2], [3, 4], [5, 6]]
[12] pry(main)> B::Foo
=> [[1, 2], [3, 4], [5, 6]]
[13] pry(main)> A::Foo
=> [[1, 2], [3, 4], [5, 6]]
[14] pry(main)> B::Foo.object_id == A::Foo.object_id
=> true
[15] pry(main)> B::Items.object_id == A::Items.object_id
=> false
您可以明确地引用父命名空间中的常数不点名使用Class#superclass
[16] pry(main)> class B; superclass::Items; end
=> [[1, 3, 5], [2, 4, 6]]
常量的范围是它们所定义的类。所以你可以重新定义子类中的常量。
我想不必重新定义整个事情(复制和粘贴)超我想我理想地寻找像Items = super.Items.concat([1,5,9]),在那里我只需要定义我的新项目,而不是超类定义的东西。 – 2013-03-04 00:24:44
常量查找并不总是超级明显,红宝石,特别是因为它通常不是作用域(例如我们写String
,不::String
)
您可以在子类中重写它。
class C < Base
CONST = build_on(superclass::CONST)
end
你必须要小心,因为你如何从实例和单方法虽然访问常数:
class Base
FOO = [42]
def self.naive
FOO
end
def naive_ins
FOO
end
def self.aware
self::FOO
end
def aware_ins
self.class::FOO
end
end
class C < Base
FOO = superclass::FOO + [:bar]
end
C.naive # => [42]
C.new.naive_ins # => [42]
C.aware # => [42, :bar]
C.new.aware_ins # => [42, :bar]
谢谢!你能链接到<<的文档吗?谷歌不喜欢它作为关键字。我使用它时遇到了问题,但是当用.concat替换时,它可以很好地工作。 – 2013-03-04 00:44:22
@EliWhite:更新了链接'<<'文档的答案。 – dbenhur 2013-03-04 00:48:27