加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby – 是否可以在一个区块内调用收益率?

发布时间:2020-12-17 02:37:48 所属栏目:百科 来源:网络整理
导读:我感兴趣是否可以这样做以及语法是什么.我在: def say_it puts "before" yield("something here") puts "after"endsay_it do |val| puts "here is " + val yield("other things") # ???end 想想可能没有,但是如果块被转换为Proc? thx提前 解决方法 只有在
我感兴趣是否可以这样做以及语法是什么.我在:

def say_it
  puts "before"
  yield("something here")
  puts "after"
end

say_it do |val|
  puts "here is " + val
  yield("other things") # ???
end

想想可能没有,但是如果块被转换为Proc?

thx提前

解决方法

只有在采用块的方法中,产量才有意义.

是的,他们可以窝.注意:

>遍历仍沿堆栈发生;和
>块(和产量)严格依赖于方法.

例:

def double(x)
    yield x * 2
end

def square_after_double(x)
    double(x) do |r|
       # Yields to the block given to the current method.
       # The location of the yield inside another block
       # does not change a thing.
       yield r * r
    end
end

square_after_double(3) do |r|
  puts "doubled and squared: " + r.to_s
end

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读