Bug #14015 ยป yield_arity.rb
1 |
TEST = %i[each find sort_by grep grep_v count detect find_index find_all select reject |
---|---|
2 |
map flat_map partition group_by all? any? one? none?
|
3 |
min_by max_by minmax_by reverse_each take_while drop_while sum uniq]
|
4 |
match_all = Object.new; def match_all.===(x); true; end |
5 |
ARGS = {grep: match_all, grep_v: /never_match/} |
6 |
class << o = Object.new |
7 |
include Enumerable |
8 |
def each |
9 |
return to_enum unless block_given? |
10 |
yield :a, 1 |
11 |
end
|
12 |
def foo(a,b) |
13 |
end
|
14 |
end
|
15 |
m = o.method(:foo) |
16 |
ok = [{a: 1}, o, {a: 1}.each, o.each].map do |receiver| |
17 |
TEST.select do |method| |
18 |
(receiver.send(method, *ARGS[method], &m); :ok) rescue false |
19 |
end
|
20 |
end
|
21 |
labels = ['Hash', 'Custom yield', 'Hash#each', 'Custom yield#each'] |
22 |
|
23 |
puts "Takes arity 2 blocks in Ruby #{RUBY_VERSION}", *labels.zip(ok.map{|l| l.join(', ')}).flatten |