任何人都可以帮助我了解“收益自我”和“收益”之间的区别?
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