rubyworks / facets

Ruby Facets

Home Page:http://rubyworks.github.com/facets

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Good definition for Array#/

trans opened this issue · 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.