More Ruby Type Weirdness

Back in My Fundamental Issue with Ruby, I complained about Ruby’s types without the ability to work with/understand types.

Just had another example of the issue. I’ve got this code:

path = ['foo', 'bar', 'baz']
msg.step = path.shift
if msg.step == 'foo'
   # Do something smart
end

The problem was that the “do something smart” line was working just fine, and then strangely wasn’t ever being hit later on. WTF?

Turns out the issue is that “msg” is an ActiveRecord instance, and “step” changed from a string type to an integer type in the database, so when I assigned a string to “step”, it silently stored it as 0.

Seriously, either be typed or don’t be typed. It hurts to sit on the fence like this.

(In more positive news, I note that Rails is really a slick system these days, with the exception of its ORM. I’m taking notes for a Grails vs. Rails blog post coming up, which I hope to be more constructive than my previous forays into the area.)

This entry was posted in To Be Categorized. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

4 Comments

  1. Mike
    Posted March 5, 2009 at 4:26 PM | Permalink

    A decent suite of tests would have warned you when you changed ’step’ from string to integer. No need to start locking everything down just to highlight the bugs in your code.

  2. Posted March 5, 2009 at 4:29 PM | Permalink

    Ruby doesn’t sit on the fence, AFAIK, at least not like Variants in e.g. VB do: the values are indeed typed. It seems to me that however ActiveRecord works, it should have interpreted the message to assign a string to the step field as a runtime error, or it should have stored it as a string and produced a runtime error later when trying to persist it.

    In other words, it looks more like a library problem – the library trying to do coercion – than a language problem.

    I may, of course, have misinterpreted the situation, given that I have done extremely little Ruby programming.

  3. Posted March 5, 2009 at 6:04 PM | Permalink

    irb(main):001:0> int_var = 0
    => 0
    irb(main):002:0> int_var = ['value'].shift
    => “value”
    irb(main):003:0> puts int_var
    value
    => nil

    Agree with Barry Kelly, this looks like ActiveRecord weirdness.

  4. Posted March 5, 2009 at 8:40 PM | Permalink

    Can you give a bit more context? This is certainly an Active Record issue, not a Ruby issue, but even then it seems that, if I understood the problem correctly, you could add validations to your model to avoid that error.

One Trackback

  1. By Ruby is the Future on January 31, 2010 at 10:25 AM

    [...] a whole lifetime of ranting against it (cite, cite, cite, cite), I finally have to eat my words, come out, and say it: Ruby is the language of the [...]

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">