Для этого вы можете использовать Метод # super_method .
module E
def initialize(e)
@e = e
end
def e
@e
end
end
module F
def initialize(f)
@f = f
end
def f
@f
end
end
class A
include E
include F
def initialize(e, f)
select_initialize(E).call e
select_initialize(F).call f
end
private
def select_initialize(mod)
self.class.
ancestors.
index(mod).
times.
reduce(method(:initialize)) { |m,_| m.super_method }
end
end
puts A.new("E", "F").f
#=> F
puts A.new("E", "F").e
#=> E
Примечание:
A.ancestors
#=> [A, F, E, Object, Kernel, BasicObject]
См. Также Модуль # предков , Массив # index , Integer # times , Enumerable # Reduce (он же inject
), Object # method и Вызов метода # .
E
и F
могут, конечно, содержать другие методы экземпляров, которые необходимы экземплярам класса.
Можно заблокировать action_name метод к любому значению, которое Вы хотите:
describe 'when called from "index" action' do
before
helper.stub!(:action_name).and_return('index')
end
it 'should do' do
helper.do_something.should == 'do'
end
end
describe 'when called from "other" action' do
before
helper.stub!(:action_name).and_return('other')
end
it 'should do' do
helper.do_something.should == 'dont'
end
end