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

ruby – 产量自身与产量之间的差异?

发布时间:2020-12-16 22:44:43 所属栏目:百科 来源:网络整理
导读:任何人都可以帮助我了解“收益自我”和“收益”之间的区别? class YieldFirstLast attr_accessor :first,:last def initialize(first = nil,last = nil) @first = first @last = last yield self if block_given? end def hello puts "#{@first} #{@last} s
任何人都可以帮助我了解“收益自我”和“收益”之间的区别?
class YieldFirstLast
    attr_accessor :first,:last

    def initialize(first = nil,last = nil)
        @first = first
        @last = last
        yield self if block_given?
    end

    def hello
        puts "#{@first} #{@last} says hello!"
    end
end

解决方法

在屈服自我的情况下,自我是传递给块的参数.简单的收益率,没有参数被传递.自己在这里不是特别的,任何事情都可以产生,例如
class Foo
  def a() yield self end
  def b() yield end
  def c() yield "Bar" end
  def d() yield 1,2,"scuba" end
  def to_s() "A!" end
end

Foo.new.a {|x| puts x } #=> A!
Foo.new.b {|x| puts x } #=> (a blank line,nil was yielded)
Foo.new.c {|x| puts x } #=> Bar
Foo.new.d {|x,y,z| puts z } #=> scuba

(编辑:李大同)

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

    推荐文章
      热点阅读