ruby ブロックを处理する关数を书く

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby ブロックを处理する关数を书く相关的知识,希望对你有一定的参考价值。

def a_method(a = 0)
  raise unless block_given?
  puts yield(0)
  puts yield(a)
  b_method(a) { yield(a) + 1 }
rescue
  puts "Error (on #{__method__})"
end

def b_method(a)
  raise unless block_given?
  puts yield(a)
  yield(a) + 1
rescue
  puts "Error (on #{__method__})"
end

p a_method(1) { |x| x + 1 }
p a_method(1) { |x| x + 5 }
=begin
1
2
3
4
5
6
7
8
=end

以上是关于ruby ブロックを处理する关数を书く的主要内容,如果未能解决你的问题,请参考以下文章