为什么STDOUT在Ruby中只显示来自一个返回的消息?

为什么STDOUT在Ruby中只显示来自一个返回的消息?,ruby,lambda,return,Ruby,Lambda,Return,我不熟悉使用Ruby。我一直在通过RubyMonk学习 我遇到了以下示例代码: def a_method lambda { return "we just returned from the block" }.call return "we just returned from the calling method" end puts a_method STDOUT生成的消息是“我们刚刚从调用方法返回”。但是,如果我删除return“我们刚刚从调用方法返回” 从代码中,消息是“我们刚从块

我不熟悉使用Ruby。我一直在通过RubyMonk学习

我遇到了以下示例代码:

def a_method
 lambda { return "we just returned from the block" }.call
 return "we just returned from the calling method"
end

puts a_method
STDOUT生成的消息是“我们刚刚从调用方法返回”。但是,如果我删除
return“我们刚刚从调用方法返回”
从代码中,消息是“我们刚从块中返回”


为什么这两条消息都不是用原始代码生成的?这是RubyMonk事件还是一直都是这样?

这是因为在第一种情况下,你吞下了lambda调用的结果。您只返回
“我们刚从调用方法”
字符串返回,该字符串放在STDOUT上

这是因为在第一种情况下,您接受了lambda调用的结果。您只返回
“我们刚从调用方法”
字符串返回,该字符串放在STDOUT上

唯一的输出是通过
放置一个_方法
,因此在输出中只能看到
一个_方法
的返回值,即
“我们刚从调用方法返回”


如果删除了
返回
行,则
a_方法
变为:

def a_method
  lambda { return "we just returned from the block" }.call
end
由于没有explicte
return
语句可用,因此
a_method
的返回值是其最后一个表达式,即lambda的返回值,即
“我们刚从块返回”


如何使两个字符串都可见?试试这个:

def a_method
  puts lambda { return "we just returned from the block" }.call
  return "we just returned from the calling method"
end

puts a_method

通过
放置
lambda的返回值,
“我们刚从块返回”
也在输出中。

唯一的输出是由
放置一个方法
完成的,因此在输出中只看到
一个方法
的返回值,即
“我们刚从调用方法返回”


如果删除了
返回
行,则
a_方法
变为:

def a_method
  lambda { return "we just returned from the block" }.call
end
由于没有explicte
return
语句可用,因此
a_method
的返回值是其最后一个表达式,即lambda的返回值,即
“我们刚从块返回”


如何使两个字符串都可见?试试这个:

def a_method
  puts lambda { return "we just returned from the block" }.call
  return "we just returned from the calling method"
end

puts a_method
通过
放置
lambda的返回值,
“我们刚从块返回”
也在输出中