@m_seki の

I like ruby tooから引っ越し

RubyでCOM呼び出しごっこ

もちろんネタです。

インターフェイスを持つかどうか調べてからメソッド呼び出すので安心なはずです。うっかり間違ったオブジェクトを与えても、例外があがったりしないんだよ。すごいでしょ。

class Object
  def query_interface(symbol)
    method(symbol) rescue nil
  end
end

def com_call(unknown)
  to_s_interface = unknown.query_interface(:to_s)
  if to_s_interface
    p to_s_interface.call
  else
    return :EFAIL
  end

  hello_interface = unknown.query_interface(:hello)
  if hello_interface
    p hello_interface.call
  else
    return :EFAIL
  end

  :S_OK
end

p com_call('String')
p com_call(:Symbol)
p com_call(['Array'])

class Foo
  def hello
    'Hello, World.'
  end
end

p com_call(Foo.new)