facets icon indicating copy to clipboard operation
facets copied to clipboard

Good definition for Array#/

Open trans opened this issue 10 years ago • 0 comments

class Array
  # Index operator.
  #
  #  a = [:a, :b, :c]
  #  (a/1)  #=> :a
  alias_method :/, :[]
end

Along time ago WhyTheLuckyStiff had defined this as:

class Array
  # Partition an array into parts of given length.
  #
  # CREDIT: WhyTheLuckyStiff ...
  def / len
    inject([]) do |ary, x|
      ary << [] if [*ary.last].nitems % len == 0
      ary.last << x
      ary
    end
  end
end

But this was deprecated b/c one could use 'each_slice(n).to_a' as of 1.9, instead.

trans avatar Apr 12 '14 19:04 trans