Not sure what the OP wants, but AFAIK #partition only ever returns two
partitions. If the task is to combine consecutive elements #each_slice works well:
On Feb 4, 10:45 am, Phlip <phlip2...@gmail.com> wrote:
> Given [1, 2, 3, 4], what's the most Rubiesque way to get [[1, 2], [3, 4]]?
>
> I had figured a variation on Array#transpose would be available, but I can't
> find one!
Chris Hulan wrote:
>> Given [1, 2, 3, 4], what's the most Rubiesque way to get [[1, 2], [3, 4]]?
> have a look at enum.partition (http://ruby-doc.org/core/classes/
> Enumerable.html#M003161)
I can't partition by value, so the closest conceptual match would be something like:
assert do
\[1, 2, 3, 7\]\.partition\_with\_index\{|p,i| i\.odd? \}\.transpose ==
\[\[1, 2\], \[3, 7\]\]
end
And that's a heckuva lot of typing, even if partition_with_index existed!
--
Phlip
Roll your own?
class Array
def regroup(count)
# do some Ruby awesomeness*
end
end
a = [1, 2, 3, 4]
[1, 2, 3, 4]
a.regroup(2)
[[1, 2], [3, 4]]
* - exercise left up to the reader
···
On Feb 4, 10:00 am, Phlip <phlip2...@gmail.com> wrote:
Have a look at Facets.Enumerable.group_by (http://facets.rubyforge.org/
quick/rdoc/core/classes/Enumerable.html#M000423)
Lots of other neat stuff there...
Cheers
···
On Feb 4, 11:00 am, Phlip <phlip2...@gmail.com> wrote:
Chris Hulan wrote:
>> Given [1, 2, 3, 4], what's the most Rubiesque way to get [[1, 2], [3, 4]]?
> have a look at enum.partition (http://ruby-doc.org/core/classes/
> Enumerable.html#M003161)
I can't partition by value, so the closest conceptual match would be something like:
assert do
[1, 2, 3, 7].partition_with_index{|p,i| i.odd? }.transpose ==
[[1, 2], [3, 7]]
end
And that's a heckuva lot of typing, even if partition_with_index existed!
Ruby-docs.org indicates Object.to_enum exists in latest 1.8.6, can you
upgrade?
···
On Feb 4, 11:32 am, Phlip <phlip2...@gmail.com> wrote:
Robert Klemme wrote:
> irb(main):001:0> [1, 2, 3, 4].to_enum(:each_slice, 2).to_a
Thanks! Now, for those of us mired in the doldrums of Ruby 1.8.x, how to write
to_enum in Ruby? I found each_slice, and I know C and ruby.h, but my Ruby
internals are rusty...
Thanks! Now, for those of us mired in the doldrums of Ruby 1.8.x, how to write to_enum in Ruby? I found each_slice, and I know C and ruby.h, but my Ruby internals are rusty...
irb(main):003:0> %w{foo bar}.each_with_index {|e,i| puts "key #{e}" if i % 2 == 0}
key foo
=> ["foo", "bar"]
irb(main):004:0> %w{foo bar}.each_with_index {|e,i| puts "key #{e}" if i & 1 == 0}
key foo
=> ["foo", "bar"]
Cheers
robert
···
On 04.02.2008 17:46, Phlip wrote:
The winner is ActiveSupport's Array#in_groups_of (calling each_slice).
Thanks y'all!
But it only won for one reason - I need the array-of-twos so I can then immediately call .each on it. in_groups_of already requires a block (and does not return the twizzled array!), so I can just use it without the each. So I would have called it each_group, but that didn't seem to catch on...
Here's why I need it. Ruby's opcodes express Hashes as strictly even-lengthed arrays, going [key1, value1, key2, value2, etc]. So I just needed to iterate those things in pairs, without excessive moduli to detect if the current item is a key or a value.
Thanks! Now, for those of us mired in the doldrums of Ruby 1.8.x, how to write to_enum in Ruby? I found each_slice, and I know C and ruby.h, but my Ruby internals are rusty...