クラスメソッド定義方法 いろいろ
クラスメソッドはselfかクラス名をメソッドの頭につけることによって作れる。
通常はクラス名変更のリファクタを考慮するからselfを使う<< は特異クラス定義をつかっておりまする
(ここまでやる必要があるかどうかは?だけどまぁ、勉強として)
http://d.hatena.ne.jp/toshi_hirasawa/20080808/1360199798
[hirasawa@centos-hira ~]$ cat classMethod.rb #!/usr/local/bin/ruby -Ku #class Hoge # def Hoge.hello() # p('hello') # end #end #class Hoge # def self.hello() #self.class.hell()は当然errorになる # p('hello') # end #end #class Hoge # # class << Hoge # def hello() # p('hello') # end # end #end #class Hoge # # class << self #class << self.classは当然errorになる # def hello() # p('hello') # end # end #end #class Hoge #end #class << Hoge # def hello() # p('hello') # end #end #class Hoge #end #class << self.class # class << selfはerrorになる # undefined method `hello' for Hoge:Class (NoMethodError) # これは謎なんで宿題 # def hello() # p('hello') # end #end class Hoge end def Hello.hello() p('hello') end Hoge.hello [hirasawa@centos-hira ~]$