transfire / gmail.com wrote:

>>>   # Built-in adaption routes that Ruby already uses in its C code.
>>>   adaption :to => Symbol,  :via => :to_sym
>>>   adaption :to => String,  :via => :to_str
>>>   adaption :to => Array,   :via => :to_ary
>>>   adaption :to => Integer, :via => :to_int
> 
> That's interesting. Is this data accessable via pure Ruby code? I was
> playing with a lib that provided these like so:
> 
>   class Symbol
>     def self.coercer ; :to_sym ; end
>     def self.convertor ; :intern ; end
>   end
> 
> Etc. that way any class could provide the information.

ruby-contract's API for providing adaptions is pretty simple. adaption() 
also supports more powerful routes. See 
http://ruby-contract.rubyforge.org/doc/classes/Kernel.html#M000019

You can also see the unit tests at 
http://ruby-contract.rubyforge.org/release/ruby-contract-0.1.1/test/tc_integration.rb 
for sample usage.

You can get a list of all adaption routes from the outside by doing 
Contract.adaptions, but I don't see a case where that would be 
necessary. For that reason the method is undocumented.

-- 
http://flgr.0x42.net/