Thanks Brian and Rob,
Yes, of course. Compact will work because the nils are always trailing. Thanks greatly.
I should have RTFB.
Now I know this is only peripherally topical, but can anyone come up with the regexp that achieves this:
string =~ pattern
[$1, $2, $3]
where string is a Roman numeral, optionall followed by "(?)" or alpha, which, if present can be followed by an Arabic number,
produces
I -> ["I", nil, nil]
I(?) -> ["I", "(?)", nil]
Ia -> ["I", "a", nil]
Ib -> ["I", "b", nil]
IIa1 -> ["II", "a", "1"]
IIa2 -> ["II", "a", "2"]
As for the trailing nils, I didn't want to deal with empty strings, but since now I'm compacting, I can use reject! instead.
I've got Dave Burt's RomanNumerals module, and it was easy to make his regexp capturing (and it will be stored as Arabic.)
/^(M*(?:D?C{0,3}|C[DM])(?:L?X{0,3}|X[LC])(?:V?I{0,3}|I[VX]))$/i
Now that the Ruby stuff is out of the way, who wants to put me out of my misery? It's not like I'm asking you to paint my house!
With interim gratitude,
Bob Schaaf
···
On Jun 5, 2009, at 4:05 PM, Brian Candler wrote:
Robert Schaaf wrote:
I have a array of foo objects, each of which contains an array bar
values. I'd like to sort the foos, ordered by the bars. The problem
is that the bars are of unequal length.
The most apt example is the numbering in an outline: 1, 1.1, 1.2,
1.2.1, 1.3 ...etc.
In practical terms, comparing 1.1 to 1.2.1 would be [1, 1, nil] <=>
[1, 2, 1]. Array#sort_by barfs on the nil. Is there some nice way to
let nil always sort low?
irb(main):002:0> [1,1] <=> [1,2,1]
=> -1
So would
sort_by { |x| x.bars.compact }
be sufficient?
--
Posted via http://www.ruby-forum.com/\.