Tag Archives: gem

Ruby Typed Accessors

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:

class Foo
float_accessor :float
date_accessor :date

>> f = Foo.new
=> #
>> f.float = “1.4”
=> “1.4”
>> f.float
=> 1.4
>> f.float = “1”
=> “1”
>> f.float
=> 1.0
>> f.date = “2009-10-30”
=> “2009-10-30”
>> f.date
=> #
>> f.date.to_s
=> “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.

Publishing my first gem: thermostat 0.0.2

When not working on OpenSim, I’ve been doing a bunch of random side projects in Ruby. One of the more recent ones has been a more advanced control and monitoring tool for my thermostat.

Along the way I created a module for the thermostat control. Last night I finally figured out how to package this work up as a gem file, which makes this easily installable to the world. Right now, the code is really raw, and I haven’t put in any documentation yet. But, for those interested, it’s available.

> gem search -r thermostat

Bulk updating Gem source index for: http://gems.rubyforge.org

thermostat (0.0.2)
    Ruby modules to control Prolipix IP Thermostats