2017-01-15から1日間の記事一覧

特異メソッドでincludeする方法

何個か方法あるので、少しづつ紹介します。 クラス内でincludeする module Foo def hello 'hello' end end class Bar class << self include Foo end end Bar.hello => "hello" 特定のオブジェクトだけincludeする x = Bar.new class << x include Foo end x…

クラス変数の注意点

クラス変数なんて、めったに使うことなんてないし、railsで使うならclass_attributeがあるんで、普通は必要ないです。 しかし、よくクラス変数やめろ!なんて言われることが多いと思うので、ここでおさらいしておきます。 クラス変数とは そもそもクラス変数…