{"id":7042,"date":"2014-05-17T00:24:16","date_gmt":"2014-05-17T00:24:16","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/05\/17\/confusion-with-super-collection-of-common-programming-errors\/"},"modified":"2014-05-17T00:24:16","modified_gmt":"2014-05-17T00:24:16","slug":"confusion-with-super-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/05\/17\/confusion-with-super-collection-of-common-programming-errors\/","title":{"rendered":"Confusion with super-Collection of common programming errors"},"content":{"rendered":"<p><strong>tl;dr:<\/strong> <code>super<\/code> behaves in unexpected ways, and variables matter, not just objects.<\/p>\n<p>When <code>super<\/code> is called, it&#8217;s not called with the object that was passed in.<\/p>\n<p>It&#8217;s called with the variable that is called <code>options<\/code> at the time of the call. For example, with the following code:<\/p>\n<pre><code>class Parent\n  def to_xml(options)\n    puts \"#{self.class.inspect} Options: #{options.inspect}\"\n  end\nend\n\nclass OriginalChild &lt; Parent\n  def to_xml(options)\n    options.merge!(:methods  =&gt; [ :murm_case_name, :murm_type_name ])\n    super\n  end\nend\n\nclass SecondChild &lt; Parent\n  def to_xml(options)\n    options = 42\n    super\n  end\nend\n\nbegin\n  parent_options, original_child_options, second_child_options = [{}, {}, {}]\n  Parent.new.to_xml(parent_options)\n  puts \"Parent options after method called: #{parent_options.inspect}\"\n  puts\n  OriginalChild.new.to_xml(original_child_options)\n  puts \"Original child options after method called: #{original_child_options.inspect}\"\n  puts\n  second_child_options = {}\n  SecondChild.new.to_xml(second_child_options)\n  puts \"Second child options after method called: #{second_child_options.inspect}\"\n  puts\nend\n<\/code><\/pre>\n<p>Which produces the output<\/p>\n<pre><code>Parent Options: {}\nParent options after method called: {}\n\nOriginalChild Options: {:methods=&gt;[:murm_case_name, :murm_type_name]}\nOriginal child options after method called: {:methods=&gt;[:murm_case_name, :murm_type_name]}\n\nSecondChild Options: 42\nSecond child options after method called: {}\n<\/code><\/pre>\n<p>You can see that with <code>SecondChild<\/code> the super method is called with the variable <code>options<\/code> which refers to a <code>Fixnum<\/code> of value <code>42<\/code>, not with the object that was originally referred to by <code>options<\/code>.<\/p>\n<p>With using <code>options.merge!<\/code>, you&#8217;d modify the hash object that was passed to you, which means that the object referred to by the variable <code>original_child_options<\/code> is now modified, as can be seen in the <code>Original child options after method called: {:methods=&gt;[:murm_case_name, :murm_type_name]}<\/code> line.<\/p>\n<p>(Note: I changed <code>options<\/code> to 42 in SecondChild, rather than call <code>Hash#merge<\/code>, because I wanted to show it wasn&#8217;t merely a case of side effects on an object)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>tl;dr: super behaves in unexpected ways, and variables matter, not just objects. When super is called, it&#8217;s not called with the object that was passed in. It&#8217;s called with the variable that is called options at the time of the call. For example, with the following code: class Parent def to_xml(options) puts &#8220;#{self.class.inspect} Options: #{options.inspect}&#8221; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-7042","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7042","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=7042"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7042\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=7042"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=7042"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=7042"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}