{"id":4322,"date":"2014-03-30T09:51:30","date_gmt":"2014-03-30T09:51:30","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/polymorphic-ruby-simpledelegator-collection-of-common-programming-errors\/"},"modified":"2014-03-30T09:51:30","modified_gmt":"2014-03-30T09:51:30","slug":"polymorphic-ruby-simpledelegator-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/polymorphic-ruby-simpledelegator-collection-of-common-programming-errors\/","title":{"rendered":"Polymorphic Ruby SimpleDelegator-Collection of common programming errors"},"content":{"rendered":"<p>What are the alternatives to the <code>SimpleDelegator<\/code> to leverage polymorphism without modifying underlying object.<\/p>\n<p>This is an example and the problem that <code>SimpleDelegator<\/code> doesn&#8217;t solve.<\/p>\n<p>The aim is to be able to wrap the original object (<code>delicious_food<\/code>) with any other (<code>yak_food<\/code>) so that the substituted method (<code>delicious?<\/code>) depends on the non-substituted methods of the underlying.<\/p>\n<pre><code>class Food\n  def initialize(color)\n    @color = color\n  end\n\n  def delicious?\n    color == :red\n  end\n\n  def color\n    @color\n  end\nend\n\nclass FoodTasteOverride &lt; SimpleDelegator\n  def color\n    :green\n  end\nend\n\ndelicious_food = Food.new(:red)\nyak_food = FoodTasteOverride.new delicious_food\n\ndelicious_food.delicious? # true - expected\nyak_food.delicious? # expecting false, but is true since the color come from delicious_food\n<\/code><\/pre>\n<p>What would be the alternative that would actually use the substituted method? The contraint is that you can&#8217;t modify the underlying object, its class or clone.<\/p>\n<p>The constraint implies that you can&#8217;t do this:<\/p>\n<pre><code>yak_food = delicious_food.clone\ndef yak_food.color\n  :green\nend\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>What are the alternatives to the SimpleDelegator to leverage polymorphism without modifying underlying object. This is an example and the problem that SimpleDelegator doesn&#8217;t solve. The aim is to be able to wrap the original object (delicious_food) with any other (yak_food) so that the substituted method (delicious?) depends on the non-substituted methods of the underlying. [&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-4322","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4322","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=4322"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4322\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4322"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4322"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4322"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}