tl; dr: super
予期しない動作をし、オブジェクトだけでなく変数も重要です。
がsuper
呼び出されると、渡されたオブジェクトでは呼び出されません。
options
これは、呼び出し時に呼び出される変数を使用して呼び出されます。たとえば、次のコードを使用します。
class Parent
def to_xml(options)
puts "#{self.class.inspect} Options: #{options.inspect}"
end
end
class OriginalChild < Parent
def to_xml(options)
options.merge!(:methods => [ :murm_case_name, :murm_type_name ])
super
end
end
class SecondChild < Parent
def to_xml(options)
options = 42
super
end
end
begin
parent_options, original_child_options, second_child_options = [{}, {}, {}]
Parent.new.to_xml(parent_options)
puts "Parent options after method called: #{parent_options.inspect}"
puts
OriginalChild.new.to_xml(original_child_options)
puts "Original child options after method called: #{original_child_options.inspect}"
puts
second_child_options = {}
SecondChild.new.to_xml(second_child_options)
puts "Second child options after method called: #{second_child_options.inspect}"
puts
end
これは出力を生成します
Parent Options: {}
Parent options after method called: {}
OriginalChild Options: {:methods=>[:murm_case_name, :murm_type_name]}
Original child options after method called: {:methods=>[:murm_case_name, :murm_type_name]}
SecondChild Options: 42
Second child options after method called: {}
スーパーメソッドでは、元々が参照されていたオブジェクトではなく、値のを参照するSecondChild
変数で呼び出されていることがわかります。options
Fixnum
42
options
を使用してoptions.merge!
、渡されたハッシュオブジェクトを変更します。これは、行に示されているように、変数によって参照されるオブジェクトがoriginal_child_options
変更されることを意味しますOriginal child options after method called: {:methods=>[:murm_case_name, :murm_type_name]}
。
(注:options
SecondChildでは、呼び出しではなく42に変更しました。Hash#merge
これは、オブジェクトに対する単なる副作用の場合ではないことを示したかったためです)