{"id":3498,"date":"2013-12-26T19:14:09","date_gmt":"2013-12-26T10:14:09","guid":{"rendered":"http:\/\/www.techscore.com\/blog\/?p=3498"},"modified":"2018-11-14T16:33:53","modified_gmt":"2018-11-14T07:33:53","slug":"ruby%e3%81%a7%e3%82%82%e5%81%bd%e8%a3%85%e5%95%8f%e9%a1%8c%ef%bc%81%ef%bc%9f%e3%82%af%e3%83%a9%e3%82%b9%e5%81%bd%e8%a3%85%e3%82%92%e8%a6%8b%e7%a0%b4%e3%82%8b","status":"publish","type":"post","link":"https:\/\/www.techscore.com\/blog\/2013\/12\/26\/ruby%e3%81%a7%e3%82%82%e5%81%bd%e8%a3%85%e5%95%8f%e9%a1%8c%ef%bc%81%ef%bc%9f%e3%82%af%e3%83%a9%e3%82%b9%e5%81%bd%e8%a3%85%e3%82%92%e8%a6%8b%e7%a0%b4%e3%82%8b\/","title":{"rendered":"ruby\u3067\u3082\u507d\u88c5\u554f\u984c\uff01\uff1f\u30af\u30e9\u30b9\u507d\u88c5\u3092\u898b\u7834\u308b"},"content":{"rendered":"<p>\u3053\u3093\u306b\u3061\u306f\u3001\u5bfa\u5ca1\u3067\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>\u8fd1\u9803\u3001\u5df7\u3092\u9a12\u304c\u305b\u3066\u3044\u308b\u507d\u88c5\u554f\u984c\u3002<br \/>\n\u305d\u308c\u306b\u3061\u306a\u3093\u3067\u3001Ruby\u3067\u898b\u304b\u3051\u308b\u507d\u88c5\u554f\u984c\u3092\u898b\u7834\u3063\u3066\u307f\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n<h2>\u507d\u88c5\u3001\u3082\u3068\u3044Proxy\u30d1\u30bf\u30fc\u30f3<\/h2>\n<p>Proxy\u30d1\u30bf\u30fc\u30f3\u3068\u547c\u3070\u308c\u308b\u30c7\u30b6\u30a4\u30f3\u30d1\u30bf\u30fc\u30f3\u304c\u3042\u308a\u307e\u3059\u3002<br \/>\n\u565b\u307f\u7815\u3044\u3066\u8aac\u660e\u3059\u308b\u3068\u300c\u5bfe\u8c61\u306e\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3063\u307d\u3044\u52d5\u304d\u3092\u3059\u308b\u3051\u3069\u3061\u3087\u3063\u3068\u3060\u3051\u30a2\u30ec\u30f3\u30b8\u3055\u308c\u3066\u308b\u3088\u300d<br \/>\n\u307f\u305f\u3044\u306a\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u4f5c\u6210\u3059\u308b\u3068\u304d\u306b\u4f7f\u308f\u308c\u307e\u3059\u3002<\/p>\n<p>Ruby\u3067\u306f\u52d5\u7684\u8a00\u8a9e\u3068\u3057\u3066\u306e\u7279\u5fb4\u3092\u6d3b\u304b\u3057\u3066\u3001method_missing\u3092\u4f7f\u3063\u305f\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u30b3\u30fc\u30c9\u3067\u5b9f\u88c5\u3055\u308c\u307e\u3059\u3002<\/p>\n<pre class=\"lang:ruby decode:true\"># to_s\u304c16\u9032\u6570\u306b\u306a\u308bInt\r\nclass HexInt &lt; BasicObject\r\n  def initialize(int)\r\n    @int = int.to_i\r\n  end\r\n\r\n  def to_s(base = 16)\r\n    @int.to_s(base)\r\n  end\r\n\r\n  def inspect\r\n    to_s\r\n  end\r\n\r\n  # \u30e1\u30bd\u30c3\u30c9\u304c\u5b58\u5728\u3057\u306a\u3044\u5834\u5408\u306b\u547c\u3070\u308c\u308b\u30e1\u30bd\u30c3\u30c9\r\n  #  @int\u306b\u51e6\u7406\u3092\u79fb\u8b72\u3059\u308b\r\n  def method_missing(method, *args, &amp;block)\r\n    val = @int.__send__(method, *args, &amp;block)\r\n    # \u623b\u308a\u5024\u304cInteger\u306a\u3089HexInt\u306b\u5909\u63db\u3059\u308b\r\n    val.kind_of?(::Integer) ? HexInt.new(val) : val\r\n  end\r\nend\r\n\r\np HexInt.new(255)\r\n=&gt; ff\r\np HexInt.new(255) + 1\r\n=&gt; 100<\/pre>\n<p>HexInt\u306e\u89aa\u30af\u30e9\u30b9\u306b\u306a\u3063\u3066\u3044\u308bBasicObject\u306f\u3001Ruby1.9\u304b\u3089\u767b\u5834\u3057\u305fObject\u30af\u30e9\u30b9\u306e\u89aa\u306e\u30af\u30e9\u30b9\u3067\u3059\u3002<br \/>\n==\u3084method_missinng\u306a\u3069\u3001ObjectClass\u306e\u57fa\u672c\u7684\u306a\u30e1\u30bd\u30c3\u30c9\u306e\u3044\u304f\u3064\u304b\u304c\u5b9a\u7fa9\u3055\u308c\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>BasicObject\u3092\u7d99\u627f\u3057\u3066\u3001method_missing\u3092\u7d99\u627f\u3059\u308b\u3053\u3068\u3067\u3001\u5225\u306e\u30af\u30e9\u30b9\uff08\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\uff09\u306b\u306a\u308a\u3059\u307e\u3059\u305f\u3081\u306e\u30af\u30e9\u30b9\u3092\u4f5c\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u306e\u3067\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n<h2>\u3069\u3093\u306a\u3068\u304d\u306b\u30cf\u30de\u308b\u306e\uff1f<\/h2>\n<p>Rails\u3067\u7279\u5b9a\u306e\u65e5\u6642\u306e1\u5e74\u5f8c\u3092\u8868\u3059DateTime\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u304c\u6b32\u3057\u3044\u5834\u5408\u3001\u4ee5\u4e0b\u306e\u30b3\u30fc\u30c9\u3067\u5b9f\u73fe\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre class=\"lang:ruby decode:true\">p DateTime.new(2013) + 1.year\r\n=&gt; Wed, 01 Jan 2014 00:00:00 +0000<\/pre>\n<p>\u3053\u306e1.year\u3068\u3044\u3046\u6570\u306f\u3001\u5f8c\u3067\u5909\u66f4\u3055\u308c\u308b\u304b\u3082\u3057\u308c\u306a\u3044\u306e\u3067\u3001\u8a2d\u5b9a\u30d5\u30a1\u30a4\u30eb\u306b\u5207\u308a\u51fa\u3059\u3053\u3068\u306b\u3057\u307e\u3057\u305f\u3002<br \/>\n\u3053\u3093\u306a\u6642\u306b\u3088\u304f\u4f7f\u3046\u306e\u304c<a href=\"https:\/\/github.com\/binarylogic\/settingslogic\">SettingsLogic<\/a>\u3068\u3044\u3046Gem\u3067\u3001\u8a2d\u5b9a\u30d5\u30a1\u30a4\u30eb\u3092YAML\u5f62\u5f0f\u3067\u6271\u3048\u307e\u3059\u3002<\/p>\n<pre class=\"decode:true\"># config\/application.yml\r\ndevelopment:\r\n  expired_after: &lt;%= 1.year %&gt;<\/pre>\n<p>\u3067\u306f\u5207\u308a\u51fa\u3057\u305f\u8a2d\u5b9a\u5024\u3092\u4f7f\u3063\u3066\u3001\u65e5\u4ed8\u3092\u8a08\u7b97\u3057\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n<pre class=\"lang:ruby decode:true\">p DateTime.new(2013) + Settings.expired_after\r\n=&gt; Sat, 11 Oct 88414 00:00:00 +0000<\/pre>\n<p>\u3042\u308c\uff1f\u304a\u304b\u3057\u306a\u65e5\u6642\u306b\u306a\u3063\u3066\u3057\u307e\u3044\u307e\u3057\u305f\u3002\u3046\u30fc\u3080\u2026\u3002<\/p>\n<pre class=\"lang:ruby decode:true\">p Settings.expired_after == 1.year               # true\r\np Settings.expired_after.class == 1.year.class   # true\r\np Settings.expired_after       # 31557600.0\r\np 1.year                       # 31557600.0\r\np Settings.expired_after.class # Float\r\np 1.year.class                 # Float<\/pre>\n<p>\u5024\u3082\u540c\u3058\u3001\u30af\u30e9\u30b9\u3082\u540c\u3058\u3001\u3069\u3046\u3057\u3066\u7d50\u679c\u304c\u9055\u3046\u306e\u3067\u3057\u3087\u3046\u304b\u3002<br \/>\n\u3082\u3046\u5c11\u3057\u8a73\u3057\u304f\u8abf\u3079\u3066\u307f\u307e\u3059\u3002<\/p>\n<pre class=\"lang:ruby decode:true\">p DateTime.new(2013) + 1.year\r\n=&gt; Wed, 01 Jan 2014 00:00:00 +0000\r\n\r\np DateTime.new(2013) + Settings.expired_after\r\n=&gt; Sat, 11 Oct 88414 00:00:00 +0000\r\n\r\np DateTime.new(2013) + 31557600.0\r\n=&gt; Sat, 11 Oct 88414 00:00:00 +0000\r\n\r\np DateTime.new(2013) + 1.year.to_f\r\n=&gt; Sat, 11 Oct 88414 00:00:00 +0000<\/pre>\n<p>1.year\u3060\u3051\u666e\u901a\u306eFloat\u3068\u7d50\u679c\u304c\u9055\u3046\u3053\u3068\u304c\u5206\u304b\u308a\u307e\u3057\u305f\u3002<\/p>\n<p>&nbsp;<\/p>\n<h2>\u5b9f\u4f53\u306f<\/h2>\n<p>\u5b9f\u306fSettings.expired_after\u30681.year\u306e\u30af\u30e9\u30b9\u306f\u540c\u3058\u3067\u306f\u3042\u308a\u307e\u305b\u3093\u3002<br \/>\nSettings.expired_after\u306fFloat\u3067\u3059\u304c\u30011.year\u306e\u5b9f\u4f53\u306fActiveSupport::Duration\u30af\u30e9\u30b9\u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u306a\u306e\u3067\u3059\u3002<br \/>\n\u8a2d\u5b9a\u30d5\u30a1\u30a4\u30eb\u306eYAML\u306bERB\u3067\u8a18\u8ff0\u3057\u305f \u300cexpired_after: &lt;%= 1.year %&gt;\u300d \u306f\u300cexpired_after: 31557600.0\u300d\u306e\u3088\u3046\u306b\u6587\u5b57\u5217\u3068\u3057\u3066\u5c55\u958b\u3055\u308c\u305f\u5f8c\u3001<br \/>\nYAML\u306e\u30d1\u30fc\u30b5\u306b\u3088\u308aFloat\u306b\u5909\u63db\u3055\u308c\u308b\u305f\u3081\u3001ActiveSupport::Duration\u30af\u30e9\u30b9\u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3067\u3042\u3063\u305f\u3068\u3044\u3046\u60c5\u5831\u306f\u5931\u308f\u308c\u3066\u3057\u307e\u3044\u307e\u3059\u3002<\/p>\n<p>\u3067\u306f\u306a\u305c\u30011.year.class\u304cFloat\u306b\u306a\u308b\u306e\u3067\u3057\u3087\u3046\u304b\u3002<\/p>\n<p>\u3053\u308c\u306f\u3001\u6700\u521d\u306b\u7d39\u4ecb\u3057\u305f BasicObject \u306b\u3088\u308b Proxy\u30d1\u30bf\u30fc\u30f3\u306e\u5b9f\u88c5\u306b\u8d77\u56e0\u3057\u307e\u3059\u3002<br \/>\n\u5148\u307b\u3069\u306eHexInt\u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u306b.class\u30e1\u30bd\u30c3\u30c9\u3092\u547c\u3073\u51fa\u3057\u305f\u5834\u5408\u3082\u3001\u7d50\u679c\u306fFixnum\u306b\u306a\u308a\u307e\u3059\u3002<br \/>\nBasicObject\u30af\u30e9\u30b9\u306b\u306fclass\u30e1\u30bd\u30c3\u30c9\u304c\u5b9a\u7fa9\u3055\u308c\u3066\u3044\u306a\u3044\u305f\u3081\u3001method_missing\u3078\u3068\u51e6\u7406\u304c\u79fb\u8b72\u3055\u308c\u3066\u3057\u307e\u3046\u306e\u3067\u3059\u3002<\/p>\n<pre class=\"lang:ruby decode:true\"># HexInt\u3067.class\u3092\u5b9f\u884c\u3057\u3066\u307f\u308b\r\n# \u5185\u90e8\u306e\u51e6\u7406\u306e\u6d41\u308c\u306f HexInt.new(255).class -&gt; HexInt.new(255).method_missing(:class) -&gt; 255.class \u3068\u306a\u308b\r\np HexInt.new(255).class\r\n =&gt; Fixnum<\/pre>\n<p>&nbsp;<\/p>\n<h2>\u30af\u30e9\u30b9\u507d\u88c5\u3092\u898b\u7834\u308b<\/h2>\n<p>BasicObject\u3092\u7528\u3044\u305fProxy\u30af\u30e9\u30b9\u306b\u3088\u308b\u507d\u88c5\u3092\u898b\u7834\u308b\u305f\u3081\u306b\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u30e1\u30bd\u30c3\u30c9\u3092\u5b9a\u7fa9\u3057\u3066\u307f\u307e\u3059\u3002<br \/>\nRuby1.8\u30681.9\u4ee5\u964d\u3067\u30b3\u30fc\u30c9\u304c\u9055\u3046\u306e\u3067\u3054\u6ce8\u610f\u304f\u3060\u3055\u3044\u3002<\/p>\n<pre class=\"lang:ruby decode:true\"># ruby 1.8 \u307e\u3067\u306e\u5834\u5408\r\ndef expose_class(object)\r\n  Object.instance_method(:class).bind(object).call\r\nend\r\n# ruby 1.9 \u4ee5\u964d\r\ndef expose_class(object)\r\n  object.instance_eval{ class &lt;&lt; self; self.superclass; end}\r\nrescue\r\n  object.class\r\nend\r\n\r\np expose_class(Settings.expired_after)\r\n =&gt; Float\r\np expose_class(1.year)\r\n =&gt; ActiveSupport::Duration<\/pre>\n<p>&nbsp;<\/p>\n<p>\u25cb\u25cb\u30af\u30e9\u30b9\u306e\u7b48\u306a\u306e\u306b\u304a\u304b\u3057\u306a\u52d5\u304d\u3092\u3059\u308b\u306a\u3041\u3001\u306a\u3069\u3068\u3044\u3046\u6642\u306f\u3001\u3053\u306e\u30e1\u30bd\u30c3\u30c9\u3092\u5229\u7528\u3057\u3066\u672c\u5f53\u306b\u305d\u306e\u30af\u30e9\u30b9\u304b\u3069\u3046\u304b\u8abf\u3079\u3066\u307f\u308b\u3068\u826f\u3044\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u3053\u3093\u306b\u3061\u306f\u3001\u5bfa\u5ca1\u3067\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>\u8fd1\u9803\u3001\u5df7\u3092\u9a12\u304c\u305b\u3066\u3044\u308b\u507d\u88c5\u554f\u984c\u3002<br \/>\n\u305d\u308c\u306b\u3061\u306a\u3093\u3067\u3001Ruby\u3067\u898b\u304b\u3051\u308b\u507d\u88c5\u554f\u984c\u3092\u898b\u7834\u3063\u3066\u307f\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002<br \/><a href=\"https:\/\/www.techscore.com\/blog\/2013\/12\/26\/ruby%e3%81%a7%e3%82%82%e5%81%bd%e8%a3%85%e5%95%8f%e9%a1%8c%ef%bc%81%ef%bc%9f%e3%82%af%e3%83%a9%e3%82%b9%e5%81%bd%e8%a3%85%e3%82%92%e8%a6%8b%e7%a0%b4%e3%82%8b\/\">\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":[22,24],"_links":{"self":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts\/3498"}],"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=3498"}],"version-history":[{"count":1,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts\/3498\/revisions"}],"predecessor-version":[{"id":4377,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts\/3498\/revisions\/4377"}],"wp:attachment":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/media?parent=3498"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/categories?post=3498"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/tags?post=3498"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}