Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Good definition for Array#/ #104

Open
trans opened this issue Apr 12, 2014 · 0 comments
Open

Good definition for Array#/ #104

trans opened this issue Apr 12, 2014 · 0 comments

Comments

@trans
Copy link
Member

trans commented Apr 12, 2014

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 trans changed the title Good definition for Array#/? Good definition for Array#/ ? Apr 12, 2014
@trans trans changed the title Good definition for Array#/ ? Good definition for Array#/ Apr 12, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant