{"id":2182,"date":"2013-03-01T09:52:38","date_gmt":"2013-03-01T00:52:38","guid":{"rendered":"http:\/\/www.techscore.com\/blog\/?p=2182"},"modified":"2018-11-14T16:33:55","modified_gmt":"2018-11-14T07:33:55","slug":"rails-include%e3%81%95%e3%82%8c%e3%81%9f%e6%99%82%e3%81%ab%e3%82%af%e3%83%a9%e3%82%b9%e3%83%a1%e3%82%bd%e3%83%83%e3%83%89%e3%81%a8%e3%82%a4%e3%83%b3%e3%82%b9%e3%82%bf%e3%83%b3%e3%82%b9%e3%83%a1","status":"publish","type":"post","link":"https:\/\/www.techscore.com\/blog\/2013\/03\/01\/rails-include%e3%81%95%e3%82%8c%e3%81%9f%e6%99%82%e3%81%ab%e3%82%af%e3%83%a9%e3%82%b9%e3%83%a1%e3%82%bd%e3%83%83%e3%83%89%e3%81%a8%e3%82%a4%e3%83%b3%e3%82%b9%e3%82%bf%e3%83%b3%e3%82%b9%e3%83%a1\/","title":{"rendered":"Rails: include\u3055\u308c\u305f\u6642\u306b\u30af\u30e9\u30b9\u30e1\u30bd\u30c3\u30c9\u3068\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u30e1\u30bd\u30c3\u30c9\u3092\u540c\u6642\u306b\u8ffd\u52a0\u3059\u308b\u983b\u51fa\u30d1\u30bf\u30fc\u30f3"},"content":{"rendered":"<p>\u3053\u3093\u306b\u3061\u306f\u3001\u9234\u6728\u3067\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>\u30e2\u30b8\u30e5\u30fc\u30eb\u306e include \u3068 extend \u306f\u6163\u308c\u308b\u307e\u3067\u306f\u3084\u3084\u3053\u3057\u3044\u3067\u3059\u306d\u3002<\/p>\n<p>\u30e9\u30a4\u30d6\u30e9\u30ea\u3092\u4f7f\u7528\u3059\u308b\u6642\u306b\u3001\u63d0\u4f9b\u3055\u308c\u3066\u3044\u308b\u30e2\u30b8\u30e5\u30fc\u30eb\u3092 include \u3059\u308c\u3070\u826f\u3044\u306e\u304b\u3001extend \u3059\u308c\u3070\u826f\u3044\u306e\u304b\u8ff7\u3046\u3053\u3068\u3082\u3042\u308b\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<p>\u3048\u3047\u30fc\u3044\u3001\u9762\u5012\u304f\u3055\u3044\uff01 \u3068\u3044\u3046\u3053\u3068\u3067\u751f\u307e\u308c\u305f\u304b\u3069\u3046\u304b\u306f\u77e5\u308a\u307e\u305b\u3093\u304c\u3001\u30e2\u30b8\u30e5\u30fc\u30eb\u306b\u95a2\u3059\u308b\u983b\u51fa\u30d1\u30bf\u30fc\u30f3\u3092\u3054\u7d39\u4ecb\u3057\u307e\u3059\u3002<\/p>\n<p>\u300c\u30e2\u30b8\u30e5\u30fc\u30eb\u304c include \u3055\u308c\u305f\u6642\u306b\u3001\u30af\u30e9\u30b9\u30e1\u30bd\u30c3\u30c9\u3068\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u30e1\u30bd\u30c3\u30c9\u306e\u4e21\u65b9\u3092\u8ffd\u52a0\u3059\u308b\u300d\u3068\u3044\u3046\u624b\u6cd5\u3067\u3059\u3002<\/p>\n<p>\u3053\u306e\u624b\u6cd5\u3092\u4f7f\u3046\u3068\u3001\u4f55\u3082\u8003\u3048\u305a\u306b include \u3059\u308c\u3070\u4fbf\u5229\u306a\u30af\u30e9\u30b9\u30e1\u30bd\u30c3\u30c9\u3084\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u30e1\u30bd\u30c3\u30c9\u304c\u4f7f\u3048\u308b\u3088\u3046\u306b\u306a\u308b\u30e2\u30b8\u30e5\u30fc\u30eb\u3092\u4f5c\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n<h2>include\u3055\u308c\u305f\u6642\u306b\u30af\u30e9\u30b9\u30e1\u30bd\u30c3\u30c9\u3068\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u30e1\u30bd\u30c3\u30c9\u3092\u540c\u6642\u306b\u8ffd\u52a0\u3059\u308b\u983b\u51fa\u30d1\u30bf\u30fc\u30f3<\/h2>\n<p>\u3053\u306e\u3088\u3046\u306a\u6642\u306e\u983b\u51fa\u30d1\u30bf\u30fc\u30f3\u3068\u3057\u3066\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u30b3\u30fc\u30c9\u304c\u66f8\u304b\u308c\u308b\u3053\u3068\u304c\u591a\u3044\u3067\u3059\u3002<\/p>\n<pre class=\"lang:ruby decode:true\">module YourModule\r\n  def self.included(base)\r\n    base.extend(ClassMethods)\r\n  end\r\n\r\n  module ClassMethods\r\n    def hello\r\n      puts 'Hello!'\r\n    end\r\n  end\r\n\r\n  def bye\r\n    puts 'Bye!'\r\n  end\r\nend<\/pre>\n<p>YourModule \u3092 include \u3059\u308b\u3068\u3001bye \u30e1\u30bd\u30c3\u30c9\u306f\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u30e1\u30bd\u30c3\u30c9\u3068\u3057\u3066\u4f7f\u3048\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002\u305d\u3057\u3066\u3001\u30cd\u30b9\u30c8\u3057\u3066\u3044\u308b ClassMethods \u30e2\u30b8\u30e5\u30fc\u30eb\u3067\u5b9a\u7fa9\u3055\u308c\u3066\u3044\u308b hello \u306f\u30af\u30e9\u30b9\u30e1\u30bd\u30c3\u30c9\u3068\u3057\u3066\u4f7f\u3048\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n<h2>\u30e2\u30b8\u30e5\u30fc\u30eb\u306e include \u3068 extend<\/h2>\n<p>\u3053\u306e\u52d5\u4f5c\u3092\u7406\u89e3\u3059\u308b\u305f\u3081\u306b\u3001\u30e2\u30b8\u30e5\u30fc\u30eb\u306e include \u3068 extend \u306b\u3064\u3044\u3066\u304a\u3055\u3089\u3044\u3057\u307e\u3057\u3087\u3046\u3002<\/p>\n<p>\u4f8b\u3068\u3057\u3066\u3001\u30ed\u30b0\u51fa\u529b\u6a5f\u80fd\u3092\u63d0\u4f9b\u3059\u308b Logging \u3068\u3044\u3046\u30e2\u30b8\u30e5\u30fc\u30eb\u304c\u3042\u308b\u3068\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:ruby decode:true\">module Logging\r\n  def log(message)\r\n    puts message\r\n  end\r\nend<\/pre>\n<p>Logging \u30e2\u30b8\u30e5\u30fc\u30eb\u3092 include \u3059\u308b\u3068\u3001\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u30e1\u30bd\u30c3\u30c9\u3068\u3057\u3066 log \u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u3048\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre class=\"lang:ruby decode:true \">class Momotaro\r\n  include Logging\r\nend\r\n\r\nmomotaro = Momotaro.new\r\nmomotaro.log('\u6843\u304b\u3089\u751f\u307e\u308c\u305f')<\/pre>\n<p>Logging \u30e2\u30b8\u30e5\u30fc\u30eb\u3092 extend \u3059\u308b\u3068\u3001\u30af\u30e9\u30b9\u30e1\u30bd\u30c3\u30c9\u3068\u3057\u3066 log \u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u3048\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre class=\"lang:ruby decode:true\">class Kintaro\r\n  extend Logging\r\nend\r\n\r\nKintaro.log('\u30de\u30b5\u30ab\u30ea\u3092\u62c5\u3044\u3060')<\/pre>\n<p>&nbsp;<\/p>\n<h2>include \u3055\u308c\u305f\u3068\u304d\u306b included \u304c\u547c\u3070\u308c\u308b<\/h2>\n<p>\u30e2\u30b8\u30e5\u30fc\u30eb\u306b included \u30e1\u30bd\u30c3\u30c9\u3092\u5b9a\u7fa9\u3057\u3066\u304a\u304f\u3068\u3001\u30e2\u30b8\u30e5\u30fc\u30eb\u304c include \u3055\u308c\u305f\u6642\u306b\u547c\u3073\u51fa\u3055\u308c\u307e\u3059\u3002<\/p>\n<pre class=\"lang:ruby decode:true\">module Logging\r\n  def self.included(base)\r\n    # (2) base \u306b\u306f Momotaro \u30af\u30e9\u30b9\u304c\u6e21\u3055\u308c\u308b\r\n  end\r\nend\r\n\r\nclass Momotaro\r\n  # (1) Momotaro \u30af\u30e9\u30b9\u304c Logging \u3092 include \u3059\u308b\u3068\r\n  include Logging\r\nend<\/pre>\n<p>included \u306e\u5f15\u6570\u306f include \u3057\u305f\u30af\u30e9\u30b9\u3084\u30e2\u30b8\u30e5\u30fc\u30eb\u3067\u3059\u3002<\/p>\n<p>\u305d\u308c\u306b\u5bfe\u3057\u3066 extend \u3059\u308c\u3070\u3001include \u3055\u308c\u305f\u3068\u304d\u306b\u30af\u30e9\u30b9\u30e1\u30bd\u30c3\u30c9\u3082\u540c\u6642\u306b\u8ffd\u52a0\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u3001\u3068\u3044\u3046\u4ed5\u7d44\u307f\u3067\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n<h2>\u307e\u3068\u3081<\/h2>\n<p>\u30e2\u30b8\u30e5\u30fc\u30eb\u306e include \u3068 extend \u306e\u52d5\u4f5c\u3001\u305d\u3057\u3066 include \u3057\u305f\u3068\u304d\u306b included \u30e1\u30bd\u30c3\u30c9\u304c\u547c\u3070\u308c\u308b\u3053\u3068\u304c\u5206\u304b\u308c\u3070\u3001\u5192\u982d\u306e\u983b\u51fa\u30d1\u30bf\u30fc\u30f3\u306e\u30b3\u30fc\u30c9\u3092\u7406\u89e3\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u306d\u3002<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u3053\u3093\u306b\u3061\u306f\u3001\u9234\u6728\u3067\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>\u30e2\u30b8\u30e5\u30fc\u30eb\u306e include \u3068 extend \u306f\u6163\u308c\u308b\u307e\u3067\u306f\u3084\u3084\u3053\u3057\u3044\u3067\u3059\u306d\u3002<br \/><a href=\"https:\/\/www.techscore.com\/blog\/2013\/03\/01\/rails-include%e3%81%95%e3%82%8c%e3%81%9f%e6%99%82%e3%81%ab%e3%82%af%e3%83%a9%e3%82%b9%e3%83%a1%e3%82%bd%e3%83%83%e3%83%89%e3%81%a8%e3%82%a4%e3%83%b3%e3%82%b9%e3%82%bf%e3%83%b3%e3%82%b9%e3%83%a1\/\">\u7d9a\u304d\u3092\u8aad\u3080...<\/a><\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[18],"tags":[22,24],"_links":{"self":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts\/2182"}],"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\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/comments?post=2182"}],"version-history":[{"count":1,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts\/2182\/revisions"}],"predecessor-version":[{"id":17949,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts\/2182\/revisions\/17949"}],"wp:attachment":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/media?parent=2182"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/categories?post=2182"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/tags?post=2182"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}