Pat Ladd started IMing me a week ago looking for a simple Ruby solution to the following problem:
- He was building objects of a SOAP web service
- SOAP in Ruby returned everything as strings
- He wanted his objects to have numbers be numbers, not strings
You could do a lot of manual conversion, but it really seemed like there should be a simple way to say “this is an int, do the conversion for me”. While ruby is dynamically typed, it is also strongly typed, so String + Int causes an exception, typically in a place you weren’t expecting.
He and I googled around for a while and found that no one seemed to have tackled this yet. Later that day Pat sent me the first version of this, all done with some fun meta programming. He and I both realized that this was probably useful enough that we should share, so I signed up to help package it as a gem.
Announcing Typed Accessors Gem
A quick example of how this works is below:
>> f = Foo.new
>> f.float = “1.4”
>> f.float = “1”
>> f.date = “2009-10-30”
The full documentation can be found on rubyforge, and the source tree on github. It’s even got unit tests as of this weekend (which already found and fixed one bug). This is released under MIT license, so do with it what you will.
If you want to use typed accessors just:
gem install typed_accessors
And require it in your environment. It works as a mixin on Class, so very seemlessly fits in your environment.