Tag: inheritance

如何在Ruby中inheritance抽象unit testing?

我有两个unit testing,应该使用略有不同的设置方法共享许多常见测试。 如果我写的东西像 class Abstract < Test::Unit::TestCase def setup @field = create end def test_1 … end end class Concrete1 < Abstract def create SomeClass1.new end end class Concrete2 < Abstract def create SomeClass2.new end end 然后Concrete1似乎没有从Abstractinheritance测试。 或者至少我不能让它们在日食中运行。 如果我为包含Concrete1的文件选择“Run all TestCases”,那么即使我不想要它也会运行Abstract。 如果我指定Concrete1那么它根本不运行任何测试! 如果我在Concrete1中指定test_1,那么它会抱怨它无法找到它(“uncaught throw:invalid_test(ArgumentError)”)。 我是Ruby的新手。 我在这里想念的是什么?

Ruby中的多重inheritance?

我认为Ruby除了mixin之外只允许单inheritance。 但是,当我有inheritance类Thing类Square时, Thing依次inheritanceObject 。 class Thing end class Square < Thing end 这不代表多重inheritance吗?

使用基类和基本模块重构ActiveRecord模型

A类和B类相同: class A < ActiveRecord::Base def foo puts "foo" end end class B < ActiveRecord::Base def foo puts "foo" end end 像这样重构与基类之间有什么区别: class Base < ActiveRecord::Base def foo puts "foo" end end class A < Base end class B < Base end 与使用基本模块的情况相比 : module Base def foo puts “foo” end end class A < […]

为什么模块的单例方法在混合的下游特征类中不可见?

我理解常规方法查找路径,即class, superclass/module, all the way up to BasicObject 。 我认为链的单例版本也是如此,但是当您在元链中混合模块时似乎并非如此。 我很感激,如果有人能解释为什么在下面的示例中,当我将此模块包含在Vehicle的本征类中时,调用Automobile模块的banner方法而不是其单例版本。 module Automobile def banner “I am a regular method of Automobile” end class << self def banner "I am a singleton method of Automobile" end end end class Vehicle def banner "I am an instance method of Vehicle" end class << self include Automobile […]

包含模块时__callee__的意外值 – 这是一个Ruby错误吗?

当通过alias_method创建的方法调用时, __callee__忽略旧方法的名称(此处为xxx )并返回新方法的名称,如下所示: class Foo def xxx() __callee__ end alias_method :foo, :xxx end Foo.new.foo # => :foo 即使从超类inheritancexxx此行为也成立: class Sup def xxx() __callee__ end end class Bar :bar 考虑到上述两种情况,我预计当通过模块包含xxx时,相同的行为将成立。 但事实并非如此: module Mod def xxx() __callee__ end end class Baz include Mod alias_method :baz, :xxx end Baz.new.baz # => :xxx 我希望返回值为:baz ,而不是:xxx 。 上面的代码是使用Ruby 2.3.1p112执行的。 这是__callee__实现中的__callee__吗? […]

在Ruby中inheritance类级实例变量?

我希望子类从其父级inheritance类级实例变量,但我似乎无法弄明白。 基本上我正在寻找这样的function: class Alpha class_instance_inheritable_accessor :foo # @foo = [1, 2, 3] end class Beta < Alpha @foo << 4 def self.bar @foo end end class Delta < Alpha @foo << 5 def self.bar @foo end end class Gamma < Beta @foo << 'a' def self.bar @foo end end 然后我想要这样输出: > Alpha.bar # [1, 2, […]

使模块inheritance自Ruby中的另一个模块

我正在为Rails创建一个小程序,其中包括我在ApplicationHelper模块内部的模块内部构建的一些方法。 这是一个例子: module Helper def time Time.now.year end end module ApplicationHelper # Inherit from Helper here… end 我知道ApplicationHelper < Helper和include Helper可以在类的上下文中工作,但是你会使用什么来进行模块到模块的inheritance? 谢谢。

Ruby mixins:扩展和包含

我一直在阅读一些关于Ruby的mixin方法, extend和include ,我仍然不太确定这种行为。 我理解, extend会将给定模块的实例方法作为单例方法添加到执行扩展的模块中,并且include将基本上将模块的内容(方法,常量,变量)附加到执行包含的模块中,有效地定义他们在接收器中。 然而,经过一些修补,试图了解行为将如何表现,我有几个问题。 这是我的测试设置: module Baz def blorg puts ‘blorg’ end end module Bar include Baz def blah puts ‘blah’ end end module Foo extend Bar end class Bacon extend Bar end class Egg include Bar end 正如我所料,模块Bar获得了Baz ( #blorg )中定义的实例方法,好像它们由于包含方法而自己定义,而类Bacon获得了单例方法Bacon::blah和Bacon::blorg通过扩展。 Bacon.blah # => blah Bacon.blorg # => blorg 而类Egg获得Bar ( #blah和现在#blorg […]

filter出现之前的顺序是什么?

filter出现之前的顺序是什么? 具体来说,关于inheiritance,before_actionfilter出现了什么顺序? 例如,这会工作: class A < ActionController::Base before_action :set_user def set_user @user = something end end class B < A before_action :set_post def show render @post end def set_post @post = @user.posts.first end end “B#show”会起作用吗? 筛选顺序有哪些规则供将来参考? 我在Rails文档中找不到任何这些内容。

inheritance如何在Ruby中工作?

根据Dave Thomas在关于Ruby对象模型的讨论中,Ruby中没有“类方法”。 方法的接收者是“类对象”还是“实例对象”之间只有区别。 class Dave def InstaceMethod ### will be stored in the current class (Dave) puts “Hi” end class << self ### Creates an eigenclass, if not created before def say_hello puts "Hello" end end end 默认情况下, ancestors方法不显示元类: class Dave class << self def metaclass ### A way to show the hidden eigenclass class […]