Object#as



try_convertを使うような場面では使えるんじゃないかな、どうかな。
ポッと思いついたときは割と良いと感じたんだけど、時間を置いたら微妙だ…

class Object
def as(converter)
v = converter.try_convert(self) or
raise ArgumentError, "#{self} is not compatible to #{converter}"
block_given? ? yield(v) : v
end
end
o = Object.new
def o.to_hash
{:foo => 123, :bar => 456}
end
o.as Hash do |h|
p h # => {:foo => 123, :bar => 456}end