{"id":6398,"date":"2012-09-12T08:22:49","date_gmt":"2012-09-12T13:22:49","guid":{"rendered":"http:\/\/bobbeaty.com\/wp\/?p=6398"},"modified":"2012-09-12T08:28:59","modified_gmt":"2012-09-12T13:28:59","slug":"ruby-include-vs-extend","status":"publish","type":"post","link":"https:\/\/bobbeaty.com\/wp\/archives\/6398","title":{"rendered":"Ruby <tt>include<\/tt> vs. <tt>extend<\/tt>"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/bobbeaty.com\/wp\/wp-content\/uploads\/2012\/07\/Ruby.jpg\" alt=\"Ruby\" title=\"Ruby.jpg\" border=\"0\" width=\"90\" height=\"90\" style=\"float:right;\" \/><\/p>\n<p>I <a href=\"http:\/\/rubyquicktips.com\/post\/1133877859\/include-vs-extend\">learned<\/a> a <em>valuable<\/em> lesson about Ruby this morning - if you have a <tt>module<\/tt> of shared code in Ruby:<\/p>\n<pre class=\"ruby\" style=\"font-family:monospace;\">  <span style=\"color:#9966CC; font-weight:bold;\">module<\/span> SharedStuff\n    <span style=\"color:#9966CC; font-weight:bold;\">def<\/span> log\n      <span style=\"color:#CC0066; font-weight:bold;\">puts<\/span> <span style=\"color:#996600;\">&quot;log&quot;<\/span>\n    <span style=\"color:#9966CC; font-weight:bold;\">end<\/span>\n  <span style=\"color:#9966CC; font-weight:bold;\">end<\/span><\/pre>\n<p>then:<\/p>\n<ul>\n<li><tt>include<\/tt> makes the module's methods available to an <em>instance<\/em> of the class<\/li>\n<li><tt>extend<\/tt> makes the methods available to the class itself<\/li>\n<\/ul>\n<p>This means that by using the same base code, you can add them in as class methods with the <tt>extend<\/tt> directive, and as instance methods with the <tt>include<\/tt> directive.<\/p>\n<p>Totally 'Ruby', as it's a strange, massive difference that might be missed by new users of the language.<\/p>\n<p>Glad I Learned it.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I learned a valuable lesson about Ruby this morning &#8211; if you have a module of shared code in Ruby: module SharedStuff def log puts &quot;log&quot; end end then: include makes the module&#8217;s methods available to an instance of the class extend makes the methods available to the class itself This means that by using [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,6],"tags":[],"class_list":["post-6398","post","type-post","status-publish","format-standard","hentry","category-coding","category-cube-life"],"_links":{"self":[{"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/posts\/6398","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/comments?post=6398"}],"version-history":[{"count":2,"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/posts\/6398\/revisions"}],"predecessor-version":[{"id":6400,"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/posts\/6398\/revisions\/6400"}],"wp:attachment":[{"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/media?parent=6398"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/categories?post=6398"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/tags?post=6398"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}