rubyでいろいろなクラス作成

個人備忘です

class Test
end

# define class method
class << Test
  def hello
    p "hello class"
  end
end

# define instance method
class Test
  def hello2
    p "hello2 instance"
  end
end

# wanna create Class by string
klass = Object.const_get("Test")
klass.class_eval do
  define_method(:hello3) do |*values|
    puts "[#{values.join(",")}]"
  end
end

obj = Test.new
Test.hello
obj.hello2
obj.hello3("by","yah")

#=>
"hello class"
"hello2 instance"
[by,yah]

http://www.yohasebe.com/files/translation/aEiMwR/
http://www.ruby-lang.org/ja/man/html/_A5AFA5E9A5B9A1BFA5E1A5BDA5C3A5C9A4CEC4EAB5C1.html#a.b1.e9.bb.bb.bb.d2.bc.b0.a4.ce.c4.ea.b5.c1