Good definition for Array#/
trans opened this issue · comments
7rans commented
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.