{"id":4104,"date":"2014-08-14T00:33:40","date_gmt":"2014-08-13T15:33:40","guid":{"rendered":"http:\/\/www.techscore.com\/blog\/?p=4104"},"modified":"2018-11-14T16:33:51","modified_gmt":"2018-11-14T07:33:51","slug":"class_include%e3%81%a8module_include","status":"publish","type":"post","link":"https:\/\/www.techscore.com\/blog\/2014\/08\/14\/class_include%e3%81%a8module_include\/","title":{"rendered":"Class#include\u3068Module#include"},"content":{"rendered":"<p>\u3044\u307e\u3055\u3089\u306a\u304c\u3089\u3001\u4eca\u5e74\u521d\u6295\u7a3f\u306e\u5bfa\u5ca1\u3067\u3059\u3002<\/p>\n<p>\u6628\u65e5\u306f<a title=\"\u795e\u6238.rb\" href=\"http:\/\/koberb.doorkeeper.jp\/\">\u795e\u6238.rb<\/a>\u306b\u304a\u3058\u3083\u307e\u3057\u3066\u3001\u3084\u3063\u3071\u308aruby\u304c\u597d\u304d\u3060\u306a\u3041\u3068\u518d\u78ba\u8a8d\u3057\u3066\u304d\u307e\u3057\u305f\u3002<br \/>\n\u3061\u3087\u3046\u3069\u30e1\u30bf\u30d7\u30ed\u306e\u30cf\u30f3\u30ba\u30aa\u30f3\u3092\u3084\u3063\u3066\u304d\u305f\u306e\u3067\u3001\u3061\u3087\u3063\u3068\u524d\u306b\u5d4c\u3063\u305f\u30e1\u30bf\u30d7\u30ed\u95a2\u9023\u306e\u5c0f\u30cd\u30bf\u3092\u66f8\u3044\u3066\u307f\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<h2>Module\u306fClass\u3060\u3051\u3067\u306a\u304f\u3001Module\u306b\u3082\u30a4\u30f3\u30af\u30eb\u30fc\u30c9\u3067\u304d\u308b<\/h2>\n<p>ruby\u306e\u30e2\u30b8\u30e5\u30fc\u30eb\u306f\u3001\u30af\u30e9\u30b9\u306b\u30a4\u30f3\u30af\u30eb\u30fc\u30c9\u3059\u308b\u3053\u3068\u3067MixIn\u3092\u5b9f\u73fe\u3067\u304d\u308b\u6a5f\u80fd\u3067\u3059\u3002<br \/>\nruby\u306e\u6700\u5927\u306e\u7279\u5fb4\u3068\u3044\u3063\u3066\u3082\u904e\u8a00\u3067\u306f\u306a\u304f\u3001\u30e1\u30bf\u30d7\u30ed\u306b\u306f\u6b20\u304b\u305b\u306a\u3044\u8981\u7d20\u3067\u3059\u306d\u3002<\/p>\n<p>\u4e00\u822c\u7684\u306b\u300c\u30e2\u30b8\u30e5\u30fc\u30eb\u306f\u30af\u30e9\u30b9\u306b\u30a4\u30f3\u30af\u30eb\u30fc\u30c9\u3059\u308b\u3082\u306e\u300d\u3068\u601d\u308f\u308c\u304c\u3061\u3067\u3059\u304c\u3001<br \/>\n\u5b9f\u306f\u30e2\u30b8\u30e5\u30fc\u30eb\u306b\u5bfe\u3057\u3066\u30e2\u30b8\u30e5\u30fc\u30eb\u3092\u30a4\u30f3\u30af\u30eb\u30fc\u30c9\u3059\u308b\u3053\u3068\u3082\u51fa\u6765\u308b\u306e\u3067\u3059\u3002<\/p>\n<pre class=\"lang:ruby decode:true \">module SayHoge\r\n  def hoge\r\n    puts \"HOGE\"\r\n  end\r\nend\r\n\r\nmodule HogeModule\r\n  # \u30e2\u30b8\u30e5\u30fc\u30eb\u306b\u30e2\u30b8\u30e5\u30fc\u30eb\u3092\u30a4\u30f3\u30af\u30eb\u30fc\u30c9\r\n  include SayHoge\r\nend\r\n\r\n# SayHoge\u306e\u6a5f\u80fd\u3092\u6301\u3063\u305fHogeModule\u3092\u30a4\u30f3\u30af\u30eb\u30fc\u30c9\r\nclass X\r\n  include HogeModule\r\nend\r\n\r\nX.new.hoge\r\n =&gt; HOGE\r\n\r\n<\/pre>\n<p>\u4e0a\u8a18\u306e\u4f8b\u306e\u3088\u3046\u306b\u3001HogeModule\u306b\u5bfe\u3057\u3066SayHoge\u30e2\u30b8\u30e5\u30fc\u30eb\u3092MixIn\u3059\u308b\u3053\u3068\u306b\u3088\u3063\u3066\u3001<br \/>\nHogeModule\u3092\u30a4\u30f3\u30af\u30eb\u30fc\u30c9\u3057\u305f\u30af\u30e9\u30b9\u306bSayHoge#hoge\u30e1\u30bd\u30c3\u30c9\u3092\u63d0\u4f9b\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3057\u305f\u3002<\/p>\n<h2>Class#include\u3068\u306f\u3061\u3087\u3063\u3068\u9055\u3046<\/h2>\n<p>Module#include\u306f\u4fbf\u5229\u306a\u6a5f\u80fd\u3067\u3059\u304c\u3001Class#include\u3068\u306f\u5927\u304d\u304f\u9055\u3046\u70b9\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<p>Class#include\u3067\u30e2\u30b8\u30e5\u30fc\u30eb\u3092\u30a4\u30f3\u30af\u30eb\u30fc\u30c9\u3057\u305f\u5834\u5408\u3001\u65e2\u306b\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u5316\u3055\u308c\u3066\u3044\u308b<br \/>\n\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306b\u5bfe\u3057\u3066\u3082\u3001\u30e2\u30b8\u30e5\u30fc\u30eb\u306e\u6a5f\u80fd\u3092\u8ffd\u52a0\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre class=\"lang:ruby decode:true \">class X\r\nend\r\n\r\nmodule HogeraModule\r\n  def hogera\r\n    \"HOGERA\"\r\n  end\r\nend\r\n\r\nx_instance = X.new\r\n\r\nclass X\r\n  include HogeraModule\r\nend\r\n\r\nX.new.hogera\r\n =&gt; HOGERA\r\nx_instance.hogera\r\n =&gt; HOGERA\r\n<\/pre>\n<p>\u540c\u3058\u3088\u3046\u306a\u611f\u899a\u3067Module#include\u3092\u4f7f\u3063\u3066\u3044\u308b\u3068\u3001\u3053\u3093\u306a\u30a8\u30e9\u30fc\u306b\u51fa\u304f\u308f\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:ruby decode:true \">module FugaModule\r\nend\r\n\r\nclass X\r\n  include FugaModule\r\nend\r\n\r\n# fuga\u30e1\u30bd\u30c3\u30c9\u3092\u63d0\u4f9b\u3059\u308bSayFuga\u30e2\u30b8\u30e5\u30fc\u30eb\r\nmodule SayFuga\r\n  def fuga\r\n    puts \"FUGA\"\r\n  end\r\nend\r\n\r\nmodule FugaModule\r\n  include SayFuga\r\nend\r\n\r\nX.new.fuga\r\n# NoMethodError:undefined method `fuga' for #\r\n<\/pre>\n<p>\u4e0a\u8a18\u306e\u3088\u3046\u306bClassX\u306b\u30a4\u30f3\u30af\u30eb\u30fc\u30c9\u3057\u305f\u5f8c\u3067FugaModule\u306b\u30e2\u30b8\u30e5\u30fc\u30eb\u3092\u30a4\u30f3\u30af\u30eb\u30fc\u30c9\u3057\u3066\u3082\u3001X\u30af\u30e9\u30b9\u306b\u306f\u53cd\u6620\u3055\u308c\u307e\u305b\u3093\u3002<\/p>\n<h2>\u8a73\u3057\u304f\u78ba\u8a8d\u3057\u3066\u307f\u308b<\/h2>\n<p>Module#include\u524d\u5f8c\u3067\u3069\u3046\u5909\u308f\u308b\u306e\u304b\u3001\u3082\u3046\u5c11\u3057\u8a73\u3057\u304f\u78ba\u8a8d\u3059\u308b\u305f\u3081\u306b\u4ee5\u4e0b\u306e\u30b3\u30fc\u30c9\u3092\u5b9f\u884c\u3057\u3066\u307f\u307e\u3059\u3002<\/p>\n<pre class=\"lang:ruby decode:true \">module BeforeInclude\r\n  def piyo\r\n    puts \"BeforeInclude\"\r\n  end\r\nend\r\n\r\nmodule AfterInclude\r\n  def piyo\r\n    puts \"AfterInclude\"\r\n  end\r\nend\r\n\r\n# BeforeInclude\u30e2\u30b8\u30e5\u30fc\u30eb\u3092\u30a4\u30f3\u30af\u30eb\u30fc\u30c9\u3057\u305fPiyoModule\u3092\u4f5c\u6210\r\nmodule PiyoModule\r\n  include BeforeInclude\r\nend\r\n\r\n# \u30af\u30e9\u30b9First\u306b\u5bfe\u3057\u3066PiyoModule\u3092\u30a4\u30f3\u30af\u30eb\u30fc\u30c9\r\nclass First\r\n  include PiyoModule\r\nend\r\n\r\n# PiyoModule\u306b\u5bfe\u3057\u3066AfterInclude\u3092\u30a4\u30f3\u30af\u30eb\u30fc\u30c9\r\nmodule PiyoModule\r\n  include AfterInclude\r\nend\r\n\r\n# Second\u30af\u30e9\u30b9\u306bAfterInclude\u304c\u30a4\u30f3\u30af\u30eb\u30fc\u30c9\u3055\u308c\u305fPiyoModule\u3092\u30a4\u30f3\u30af\u30eb\u30fc\u30c9\r\nclass Second\r\n  include PiyoModule\r\nend\r\n\r\nFirst.new.piyo\r\n =&gt; BeforeInclude\r\n\r\nFirst.ancestors\r\n =&gt; [First, PiyoModule, BeforeInclude, Object, Kernel, BasicObject]\r\n\r\nSecond.new.piyo\r\n =&gt; AfterInclude\r\n\r\nSecond.ancestors\r\n =&gt; [Second, PiyoModule, AfterInclude, BeforeInclude, Object, Kernel, BasicObject]\r\n\r\n<\/pre>\n<p>\u540c\u3058PiyoModule\u3092\u30a4\u30f3\u30af\u30eb\u30fc\u30c9\u3057\u3066\u3044\u308b\u306e\u306b\u3001piyo\u30e1\u30bd\u30c3\u30c9\u306e\u5b9f\u884c\u7d50\u679c\u304c\u9055\u3044\u307e\u3059\u306d\u3002<br \/>\nFirst\u3068Second\u306b\u5bfe\u3059\u308bancestors\u306e\u7d50\u679c\u3092\u6bd4\u3079\u308b\u3068\u3001Second\u3060\u3051AfterInclude\u30e2\u30b8\u30e5\u30fc\u30eb\u304c\u8ffd\u52a0\u3055\u308c\u3066\u3044\u308b\u306e\u304c\u308f\u304b\u308a\u307e\u3059\u3002<\/p>\n<p>Module#include\u306f\u300c\u30e2\u30b8\u30e5\u30fc\u30eb\u304c\u30a4\u30f3\u30af\u30eb\u30fc\u30c9\u3055\u308c\u308b\u969b\u306b\u3001\u8ffd\u52a0\u3067\u30a4\u30f3\u30af\u30eb\u30fc\u30c9\u3059\u308b\u30e2\u30b8\u30e5\u30fc\u30eb\u3092\u6307\u5b9a\u3059\u308b\u300d\u3068\u3044\u3046\u6319\u52d5\u306b\u306a\u308a\u307e\u3059\u3002<br \/>\n\u305d\u306e\u52b9\u679c\u306f\u65e2\u306b\u30e2\u30b8\u30e5\u30fc\u30eb\u304c\u30a4\u30f3\u30af\u30eb\u30fc\u30c9\u3055\u308c\u305f\u30af\u30e9\u30b9\u306b\u306f\u6ce2\u53ca\u3057\u307e\u305b\u3093\u3002<\/p>\n<h2>Module#include\u3092\u4f7f\u3046\u3068\u304d\u306f\u9806\u5e8f\u306b\u6ce8\u610f<\/h2>\n<p>Module#include\u306f\u8907\u96d1\u306a\u30e2\u30b8\u30e5\u30fc\u30eb\u3092\u5206\u5272\u3059\u308b\u3068\u304d\u306b\u4fbf\u5229\u3067\u3059\u304c\u3001<br \/>\n\u5229\u7528\u3059\u308b\u5834\u5408\u306f\u5b9f\u884c\u3059\u308b\u30e2\u30b8\u30e5\u30fc\u30eb\u304c\u307e\u3060\u4ed6\u306e\u30af\u30e9\u30b9\u306b\u30a4\u30f3\u30af\u30eb\u30fc\u30c9\u3055\u308c\u3066\u3044\u306a\u3044\u30bf\u30a4\u30df\u30f3\u30b0\u3067\u5b9f\u884c\u3059\u308b\u3088\u3046\u306b\u8abf\u6574\u3057\u305f\u307b\u3046\u304c\u826f\u3044\u3067\u3057\u3087\u3046\u3002<br \/>\n\u65e2\u5b58\u306e\u30e9\u30a4\u30d6\u30e9\u30ea\u3078\u306e\u30e2\u30f3\u30ad\u30fc\u30d1\u30c3\u30c1\u306a\u3069\u306e\u7528\u9014\u3067Module#include\u3092\u4f7f\u3046\u306e\u306f\u304a\u30b9\u30b9\u30e1\u3067\u304d\u307e\u305b\u3093\u3002<\/p>\n<p>\u3082\u3061\u308d\u3093Module#prepend\u3082\u540c\u3058\u3088\u3046\u306a\u6319\u52d5\u306b\u306a\u308b\u306e\u3067\u3054\u6ce8\u610f\u3092\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u3044\u307e\u3055\u3089\u306a\u304c\u3089\u3001\u4eca\u5e74\u521d\u6295\u7a3f\u306e\u5bfa\u5ca1\u3067\u3059\u3002<\/p>\n<p>\u6628\u65e5\u306f\u795e\u6238.rb\u306b\u304a\u3058\u3083\u307e\u3057\u3066\u3001\u3084\u3063\u3071\u308aruby\u304c\u597d\u304d\u3060\u306a\u3041\u3068\u518d\u78ba\u8a8d\u3057\u3066\u304d\u307e\u3057\u305f\u3002<br \/>\n\u3061\u3087\u3046\u3069\u30e1\u30bf\u30d7\u30ed\u306e\u30cf\u30f3\u30ba\u30aa\u30f3\u3092\u3084\u3063\u3066\u304d\u305f\u306e\u3067\u3001\u3061\u3087\u3063\u3068\u524d\u306b\u5d4c\u3063\u305f\u30e1\u30bf\u30d7\u30ed\u95a2\u9023\u306e\u5c0f\u30cd\u30bf\u3092\u66f8\u3044\u3066\u307f\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002<br \/><a href=\"https:\/\/www.techscore.com\/blog\/2014\/08\/14\/class_include%e3%81%a8module_include\/\">\u7d9a\u304d\u3092\u8aad\u3080...<\/a><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[18],"tags":[24],"_links":{"self":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts\/4104"}],"collection":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/comments?post=4104"}],"version-history":[{"count":23,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts\/4104\/revisions"}],"predecessor-version":[{"id":4317,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts\/4104\/revisions\/4317"}],"wp:attachment":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/media?parent=4104"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/categories?post=4104"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/tags?post=4104"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}