Ruby の問題です。下記の記事の続編ですが,少し難しくなっています。
m{|x| x } が 0 で m{|x, y| x * y } が 1 だと? - Qiita
以下のテストをパスするメソッド m
を定義せよ。
require "test/unit"
class T < Test::Unit::TestCase
test "" do
assert_equal 0, m(&->(x, y){ x })
assert_equal 1, m(&->(x, y){ x * y })
end
end
Ruby 2.7 以降: Ruby 2.6 まで: などとする。答えはここをめくってね
def m
yield 0, "".tap{ def _1.coerce(*); [1, 1]; end }
end
def m
yield 0, "".tap{ |o| def o.coerce(*); [1, 1]; end }
end