views:

198

answers:

2

Hi!

Im trying to test my rails applications javascript using jruby 1.3.1,celerity and culerity. The application itself runs under ruby 1.8.7 + phusion passenger (and runs fine, sans test :))

Everything installation-wise works fine but my app uses some_enumerable.each_slice(10) to split a larger array into smaller subarray with 10 elelents each.

Celerity need jruby and jruby is only ruby 1.8.6 compatible and therefor doesnt support a blockless each_slice.

So I'm thinking about defining an initalizer which adds this functionality if RUBY_PLATFORM == "java " (or RUBY_VERSION < 1.8.7)

This far I got (defunct code of cause):

if true #ruby 1.8.6
module Enumerable
  alias_method :original_each_slice, :each_slice
  def each_slice(count, &block)
    # call original method in 1.8.6
    if block_given?
      original_each_slice(count, block)
    else
      self.enum_for(:original_each_slice, count).to_a
    end
  end
end
end

This code obviously is not working and I would really appreciate someone pointing me to a solution.

Thanks!

Update: Solution thanks to sepp2k for pointing me to my errors:

if RUBY_VERSION < "1.8.7"
  require 'enumerator'
  module Enumerable
    alias_method :original_each_slice, :each_slice
    def each_slice(count, &block)
      if block_given?
        # call original method when used with block
        original_each_slice(count, &block)
      else
        # no block -> emulate
        self.enum_for(:original_each_slice, count)
      end
    end
  end
end
+1  A: 

original_each_slice(count, block) should be original_each_slice(count, &block).

Also if you leave out the to_a, you'll be closer to the behaviour of 1.8.7+, which returns an enumerator, not an array.

(Don't forget to require 'enumerator' btw)

sepp2k
+1  A: 

checkout the 'backports' gem :)

rogerdpack
DOH! :) Should have searched first
Frank Schumacher