Skip to content

Commit 81b8325

Browse files
committedMar 20, 2014
Add Enumerable#index_by
1 parent 1f24442 commit 81b8325

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
 
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
module Enumerable
2+
def index_by(&block)
3+
return enum_for :index_by unless block_given?
4+
5+
hash = Hash.new
6+
7+
%x{
8+
var result;
9+
10+
self.$each._p = function() {
11+
var param = #{Opal.destructure(`arguments`)},
12+
value = $opal.$yield1(block, param);
13+
14+
if (value === $breaker) {
15+
result = $breaker.$v;
16+
return $breaker;
17+
}
18+
19+
#{hash[`value`] = `param`};
20+
}
21+
22+
self.$each();
23+
24+
if (result !== undefined) {
25+
return result;
26+
}
27+
}
28+
29+
hash
30+
end
31+
32+
end

0 commit comments

Comments
 (0)
Please sign in to comment.