[英]What's the difference between `puts` and `return` result?
When I run the following code: 当我运行以下代码时:
class GermanShepard
attr_accessor :name
def bark
puts "Loud Bark"
end
end
max = GermanShepard.new
max.name = "Max"
puts "#{max.name} goes #{max.bark}"
the result is: 结果是:
Loud Bark
Max goes
When I change puts
to return
at the GermanShepard
. 当我改变
puts
,以return
在GermanShepard
。 It gives: 它给:
Max goes Loud Bark
I don't understand the difference between the two command in the class. 我不明白这两个命令之间的区别。
In most languages, return
in a method means giving a value to the caller. 在大多数语言中,方法中的
return
表示为调用方提供值。
In Ruby, everything is an expression . 在Ruby中,一切都是表达式 。 In an essence, the last line of a method is automatically called with
return
. 本质上,方法的最后一行是使用
return
自动调用的。 As an example, the following methods are equivalent: 例如,以下方法是等效的:
def bark_a
'Woof!'
end
def bark_b
return 'Woof!'
end
However, some methods may not be return
ing anything. 但是,某些方法可能不会
return
任何东西。
def bark_c
end
In this case, ruby is actually returning a nil
object. 在这种情况下,ruby实际上返回一个
nil
对象。 An example would be the 一个例子是
puts
method. puts
方法。 The puts
method simply displays whatever you've given it. puts
方法仅显示您提供的任何内容。
So in your example, 所以在你的例子中
def bark
puts "Loud Bark"
end
is actually doing 2 things. 实际上是在做两件事。
puts
method (displaying Loud Bark
to the terminal screen) puts
方法(在终端屏幕上显示Loud Bark
) nil
value back to the method caller. nil
值返回给方法调用者。 You can try running puts nil
and see what's printed out! 您可以尝试运行
puts nil
并查看打印出来的内容!
Calling #{}
in ruby is called interpolation , which is basically putting the variables together in their closest string
representation value. 在ruby中调用
#{}
称为内插 ,基本上是将变量放到它们最接近的string
表示值中。 The following statements are roughly equivalent: 以下语句大致等效:
puts "One plus one equals #{1 + 1}"
puts "One plus one equals " + (1 + 1).to_s
With all the information above, we can now go through your example step-by-step. 有了上述所有信息,我们现在就可以逐步完成您的示例。
The line puts "#{max.name} goes #{max.bark}"
can be separated into a few steps 这行代码
puts "#{max.name} goes #{max.bark}"
可以分为几步
name
method name
方法 return
ed is converted into the closest string
representation (In this case we don't need to do anything since name
is already a string
) return
ed的值return
转换为最接近的string
表示形式(在这种情况下,由于name
已经是一个string
我们无需执行任何操作) At this point, our temporary variable is Max goes
. 此时,我们的临时变量是
Max goes
。
bark
method bark
方法 puts "Loud Bark"
gets executed since we're calling it in the bark
method. puts "Loud Bark"
,因为我们在bark
方法中调用了它。 puts
method returns a nil
value, the bark
method is also going to return
a nil
value because it's the last line of the bark
method. puts
方法返回nil
值,因此bark
方法也将return
nil
值,因为它是bark
方法的最后一行。 (Read "Expression" above) nil
is then converted to the closest string
representation (which is "") nil
转换为最接近的string
表示形式(即“”) The temporary variable is now Max goes
现在临时变量是
Max goes
Now, the temporary variable is passed into the puts
method, and the terminal(console) displays "Max goes " 现在,将临时变量传递到
puts
方法中,并且终端(控制台)显示“ Max gos”
Program finishes 程序完成
Therefore, your example prints out 因此,您的示例打印出来了
Loud Bark
Max goes
However, if we change the puts
inside the bark
method to return
, 但是,如果我们
puts
bark
方法中的puts
更改为return
,
Step 6 will not happen. 步骤6不会发生。
Instead of return
ing a nil
value in step 7, it will return
"Load bark"
代替在步骤7中
return
nil
值,它将return
"Load bark"
Step 8 will not happen since "Loud bark"
already a string
value 由于
"Loud bark"
已经是string
值,因此不会发生步骤8
and the temporary value after step 9 will become Max goes Loud bark
并且第9步之后的临时值将变为
Max goes Loud bark
Hope this helps! 希望这可以帮助!
puts(string)
in ruby writes the string value into $stdout
. ruby中的
puts(string)
将字符串值写入$stdout
。 For example if you run ruby console in terminal, string will be written into your terminal. 例如,如果您在终端中运行ruby console,则字符串将被写入到终端中。 At the same time every method in ruby returns something and method
puts
returns nil
. 同时,ruby中的每个方法都返回某些内容,而
puts
方法则返回nil
。 So, when we want to interpolate string like "Hello #{some_method}" it will interpolate a returning value of some_method
, in case of puts
it will interpolate nil
value. 因此,当我们要插值“ Hello#{some_method}”之类的字符串时,它将插值返回的
some_method
值,如果puts
,则会插值nil
值。
Loud Bark # output of max.bark method
Max goes # output of last puts
Ray's answer is probably correct, but since it is too verbose (and I didn't even feel like reading it), I will post a concise answer. Ray的答案可能是正确的,但是由于它太冗长了(我什至不喜欢阅读它),所以我将发布一个简洁的答案。
There are two things to have in mind. 有两件事要牢记。
With your code as is, the argument of puts
in 对于您的代码,
puts
的参数
puts "#{max.name} goes #{max.bark}"
is first evaluated. 首先被评估。 In order to do so,
max.bark
(among other things like max.name
) would have to be evaluated in advance. max.name
,必须事先评估max.bark
(例如max.name
)。 The method bark
prints "Loud Bark"
, then returns nil
. bark
方法打印"Loud Bark"
,然后返回nil
。 So the argument mentioned above becomes "#{"Max"} goes #{nil}"
, and eventually to "Max goes "
. 因此,上面提到的参数变为
"#{"Max"} goes #{nil}"
,最终变为"Max goes "
。 That is printed after "Loud Bark"
. 在
"Loud Bark"
之后打印。
When you change the puts
in the method to return
, the method bark
will have no side effect, and returns "Loud Bark"
; 当您将
puts
方法更改为return
, bark
方法将没有副作用,并返回"Loud Bark"
; that is the function of return
(and by the way, return
is not a method but is a keyword). 那就是
return
的功能(顺便说一句, return
不是方法而是关键字)。 So the argument mentioned above becomes "#{"Max"} goes #{"Loud Bark"}"
, and eventually to "Max goes Loud Bark"
. 因此,上面提到的参数变为
"#{"Max"} goes #{"Loud Bark"}"
,最终变为"Max goes Loud Bark"
。 That would be the only thing printed. 那将是唯一印出来的东西。
puts "Loud Bark" # => returns nil
return "Loud Bark" # => returns "Loud Bark"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.