rubys perlness

Tue, Dec 16, 2008

So I’ve got quite far with FlikBak now, drilling down to individual photo URLs for the original sizes but don’t you notice when you’re learning a new language that you start off really slowly, saying “eh?, WTF? why?” a lot, then you start to crawl, then you run, really fast. Then you go “waaaaaaaa!” and fall into a big hole and sit at the bottom saying “WTF?”. Well, this has just happened! I added a new class, PhotoSize to the ORM stuff I’m doing to map from Flickr’s XML responses to Ruby classes. My brain works that way, so it’s easier to code. However, this line:

attr :candownload, :canblog, :canprint
produced this error:
ArgumentError: wrong number of arguments (3 for 2)
method attr in photosize.rb at line 3
I read that attr: is equivalent to attr_reader + attr_writer. Well it is but it’s not. You see attr* are not Ruby keywords, they’re Module methods. I’d been using attr: blah in the previous project and getting away with it for the simple reason it was “attr: blah”. Now I was chaining them, as, apparantly attr = attr_reader+attr_writer. But it’s not! From the Module doc:
attr(symbol, writable=false) => nil
and there we have it. It takes, two args, not one. So you can’t chain attr definitions. But the error it gave took me back to the hideous days of Perl, which I don’t use any more, if I can avoid it, where you can call functions with or without brackets and they behave differently depending what convention you use. I didn’t expect this nonsense in Ruby but there it is, in black and white!

comments powered by Disqus