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.)
4 Comments
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.
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.
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.
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
[...] 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 [...]