Skip to content

Commit

Permalink
Add Enumerable#index_by
Browse files Browse the repository at this point in the history
  • Loading branch information
elia committed Mar 20, 2014
1 parent 1f24442 commit 81b8325
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions opal/active_support/core_ext/enumerable.rb
@@ -0,0 +1,32 @@
module Enumerable
def index_by(&block)
return enum_for :index_by unless block_given?

hash = Hash.new

%x{
var result;
self.$each._p = function() {
var param = #{Opal.destructure(`arguments`)},
value = $opal.$yield1(block, param);
if (value === $breaker) {
result = $breaker.$v;
return $breaker;
}
#{hash[`value`] = `param`};
}
self.$each();
if (result !== undefined) {
return result;
}
}

hash
end

end

0 comments on commit 81b8325

Please sign in to comment.