Support for nil values in Enumerable.min or max

Posted by admin, Thu Sep 06 06:55:44 UTC 2007

My build failed today, after I added some new code to find the min and max value from inside a Hash.  My hash was keeping track of x,y pairs, and I needed to find the min and max of the x’s.

min = data_series.min {|a,b| a[1][0] <=> b[1][0] } max = data_series.max {|a,b| a[1][0] <=> b[1][0] } Looks pretty simple. But sometimes, I had nil values for the x’s. At which point you get treated to the, not very descriptive, error message:
ArgumentError: comparison of Array with Array failed

I tried a few things like:

min = data_series.min {|a,b| a[1][0] <=> b[1][0] unless a[1][0].nil? or b[0][1].nil? }

But that didn’t work either. After some Googling, and reading some documentation for a completely different Enumerable method, the answer occurred to me. The block CAN’T return nil. It has to return one of the Comparable values of -1, 0, or 1. So:

min = data_series.min {|a,b| (a[1][0].nil? or b[1][0].nil?) ? 0 : a[1][0] < => b[1][0] }

And, finally, that worked. The lesson learned here is that the block versions of max and min MUST return -1, 0, or 1. Remember that next time, Lori.

0 comments | Filed Under: | Tags:

JRuby and the last mile

Posted by admin, Sun Sep 02 05:14:16 UTC 2007



Headius: Java Native Access + JRuby = True POSIX

but the result will be true POSIX functionality in JRuby…the “last mile” of compatibility will largely be solved.


I don’t normally just post links to other blog postings, but this is a very important milestone in the evolution of JRuby, and is very exciting.

0 comments | Filed Under: | Tags:

Be careful with your Array initializers

Posted by admin, Thu Aug 23 07:59:28 UTC 2007

This one had me going for couple of hours today. I wanted to segregate some data into “buckets”. So I initialized my array of arrays (buckets), and then proceeded to add stuff to the individual buckets. But my unit test was failing, because, for a system of 10 buckets, and me adding 1 thing, I always got back 10 things. Weird. But look at this:

$ irb irb(main):001:0&gt; foo = Array.new(10, []) =&gt; [[], [], [], [], [], [], [], [], [], []] irb(main):003:0&gt; foo[0] &lt;&lt; 1 =&gt; [1] irb(main):004:0&gt; foo =&gt; [[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]] irb(main):005:0&gt;

WTF? So, that array that I initialized my buckets with? The same array. Add to one, add to all. Grrr.

Now, I know this is in the PickAxe book, in the RDocs for Array.new… But I never read that far, ok?

So, for the confused, the correct answer is to use a block initializer like so: irb(main):008:0&gt; foo = Array.new(10) { [] } =&gt; [[], [], [], [], [], [], [], [], [], []] irb(main):009:0&gt; foo[0] &lt;&lt; 1 =&gt; [1] irb(main):010:0&gt; foo =&gt; [[1], [], [], [], [], [], [], [], [], []] irb(main):011:0&gt;

0 comments | Filed Under: | Tags:

Almost 35 years since anyone walked on the moon

Posted by admin, Fri Jul 20 04:57:21 UTC 2007

July 20, 1969: One Small Step … One Giant Leap …

Five more Apollo missions carried astronauts to the moon before the program ended in 1972. (There was to have been six, but Apollo 13’s mission ended in near disaster.) The last man to leave his footprint on the moon was Apollo 17 commander Eugene Cernan, on Dec. 14, 1972.

Considering that watching the moonwalks was one of the most exciting things I did in elementary school, I find it sad that we have a couple of generations going now, who never had that privilege. I hope that NASA, or even the private sector, can find a way to get there again soon.

0 comments | Filed Under: | Tags:

Alberta politicians need a reality check

Posted by admin, Fri Jul 13 03:42:02 UTC 2007


Cream pie protests ‘thoroughly unacceptable,’ Calgary mayor says

The latest Alberta politician to be the target of a cream pie says he’s disappointed the dessert has become “the method of choice” of those who want to make a political statement.


I mean, really. These people could be coming after the politicians with guns. Instead it’s a pie in the face. I think it says something, something nice, about our political system, and our residents. The fact that you are having them charged with “assault with a weapon”... means that you don’t understand what a weapon actually is.

0 comments | Filed Under: | Tags:

The perils of Stampede

Posted by admin, Fri Jul 13 03:36:56 UTC 2007

See… stampeding is dangerous!

Mechanical bull knocks Calgary man unconscious

A young Calgary man is in hospital with potentially life-threatening head injuries after an accident with a mechanical bull.

For those of you reading who don’t live in Calgary, or who have never come to the Calgary Stampede… “stampeding” is a verb, which refers to all the different kinds of fun activities, usually combined with consuming too much alcohol, that you can do while attending the Calgary Stampede.

1 comment | Filed Under: | Tags:

Helpful government employee, not an oxymoron after all

Posted by admin, Fri Jun 22 03:00:28 UTC 2007

I sat down at my desk today, dreading the task ahead. The increasing shrill and threatening notices I have been receiving from the Canada Revenue Agency (CRA) really had to be dealt with today. But I wasn’t looking forward to it.

You see, I made such a simple mistake almost a year ago. I have my own company, 100% owned by me, and through it I do my consulting work. I pay myself as an employee. Under the CRA rules, that makes me EI exempt (I don’t pay Employment Insurance, nor does my company, because I’m self-employed). Last year, in the summer, I was using the strange little application that the CRA puts out called TOD (for historical reasons, Tables On Diskette), which calculates the payroll withholdings (Federal and Provincial Taxes, IE deductions, Canada Pension Plan deductions, etc). And I missed the little “EI exempt” checkbox. Once. So, I ended up submitting an EI deduction to the government for one month.

At T4 time (yearly payroll tax summary/receipt) in February, I noticed all this, and submitted the T4 summary, corrected for the mistake in the EI deduction. Or so I thought. In April, the CRA started sending me these nasty notes about how I was ripping them off for the EI deductions from last year, and I now owed them about $1500.

Argh.

It’s been a VERY busy few months, so I was ignoring this, but the last round of forms started adding interest to the $1500 assessment, so I figured it was past time to get it straightened out. I pulled out all the paperwork, started up NeoOffice, and started writing a letter to the CRA, to explain all this. Then I noticed the “If you need help with this review, please call…” on the last form they sent. Hmmm. What if I could just call and fix this? It was worth a shot, wasn’t it?

After a simple two-level IVR phone system, in under a minute I was talking to a real person. She brought up my account, and I gave her the sad story. I expected a run-around. I expected to have to mail and/or FAX a bunch of supporting documents to correct this problem. What I didn’t expect was this:

She said, “Don’t worry about it. This was obviously a mistake. Don’t respond to any of the assessment notices, or fill out any of the forms. I’ll take care of it.”

... here’s me at my desk with my jaw sitting down on the floor…

I’m afraid I didn’t get your name, Ms. CRA Employee, but you made my whole day, and I thank you for it!

1 comment | Filed Under: | Tags:

Easy updates in RJS

Posted by admin, Thu Jun 21 14:55:25 UTC 2007

Since I started programming in Ruby I’ve discovered that sometimes knowing how to do something is not the same thing as knowing how to do it right.

In the process of creating a fairly complex control for multi-select, I was struggling with an Ajax callback action, in which, among other things, I was trying to update a hidden field.  In RJS, when I want to update a field, I pull out the trusty page[‘hidden_field_id’].replace, and off I go.  But, I just couldn’t make it work in my Ajax callback, because I didn’t have access to the same instance variable that I had in the view when the hidden field was originally created.  And suddenly I’m diving into some really esoteric stuff, like define_method, because there’s gotta be a define_instance, doesn’t there?  Dead-ending there, I stumble into ActionView::Base::ObjectWrapper.new.  That’s gotta work, right?  Not really, at least I couldn’t quite figure it out.  I wasted an hour or so on this, and then wrapped up for the day.  On the drive home, I remembered reading about some RJS that could update the class of a DOM element.  Hmmm.  Sounds more promising.  Some Googling from home revealed this:

#3084 ([PATCH] Change css-class to DOM-elements in rjs) – Rails Trac – Trac

page[‘my_id’].className = ‘new_class_name’


And the brain-fart ended. I put away page.replace, and wrote:

page[‘hidden_field_id’].value = my_new _value

Which was what I was after, anyway.  Much simpler than replacing the entire DOM element.  Sigh.  When will I learn?  There’s ALWAYS a simpler way.

0 comments | Filed Under: | Tags:

Floods? Really?

Posted by admin, Fri Jun 15 01:34:06 UTC 2007

I was reading my RSS feeds this morning, and I checked the weather forecast.  This is what I saw:



So… what exactly do you do with a forecast of “Floods”?

0 comments | Filed Under: | Tags: