Rubyでは、Module関数を沢山定義する時に便利な Module.module_function というものがあります。以下のように使えます。

   1  module Foo
   2    def foo; puts "foo"; end
   3  module_function
   4    def bar; puts "bar"; end
   5    def baz; puts "baz"; end
   6  end
   7  
   8  Foo.foo # => NoMethodError: undefined method `foo' for Foo:Module
   9  Foo.bar # => bar
  10  Foo.baz # => baz

module_functionの定義を終了したい場合にはどうやるのかな、と漠然と思っていたのですが、以下のようにすれば良いようです。

   1  module Foo
   2  module_function
   3    def bar; puts "bar"; end
   4    def baz; puts "baz"; end
   5  public
   6    def foo; puts "foo"; end
   7  end
   8  
   9  Foo.foo # => NoMethodError: undefined method `foo' for Foo:Module

class関数用のclass_functionもあるのかなと思ったら、無いようです。 classではmodule_functionを使う事も出来ないみたいです。

posted by Png genki on Sat 3 Jan 2009 at 13:33

Comments:

or Preview
Social Bookmarks
  • Delicious
  • B_entry1177
  • Clip_16_12_w
Services from s21g
twpro(ツイプロ)
Twitterプロフィールを快適検索
地価2009
土地の値段を調べてみよう
MyRestaurant
自分だけのレストラン手帳
Formula
ブログに数式を埋め込める数式コミュニティ