简体   繁体   English

“ puts”和“ return”结果之间有什么区别?

[英]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 ,以returnGermanShepard It gives: 它给:

Max goes Loud Bark

I don't understand the difference between the two command in the class. 我不明白这两个命令之间的区别。

Expression 表达

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. 实际上是在做两件事。

  1. It's calling the puts method (displaying Loud Bark to the terminal screen) 它正在调用puts方法(在终端屏幕上显示Loud Bark
  2. then it's giving a 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

Your example 你的例子

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}"可以分为几步

  1. Calls the name method 调用name方法
  2. The value 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我们无需执行任何操作)
  3. The value is then saved to a temporary variable 然后将该值保存到一个临时变量

At this point, our temporary variable is Max goes . 此时,我们的临时变量是Max goes

  1. Calls the bark method 调用bark方法
  2. The line puts "Loud Bark" gets executed since we're calling it in the bark method. 该行将执行puts "Loud Bark" ,因为我们在bark方法中调用了它。
  3. Terminal(console) displays "Loud Bark" 终端(控制台)显示“ Loud Bark”
  4. Since the 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) (阅读上面的“表达式”)
  5. nil is then converted to the closest string representation (which is "") 然后将nil转换为最接近的string表示形式(即“”)
  6. It is then saved to a temporary variable 然后将其保存到一个临时变量

The temporary variable is now Max goes 现在临时变量是Max goes

  1. Now, the temporary variable is passed into the puts method, and the terminal(console) displays "Max goes " 现在,将临时变量传递到puts方法中,并且终端(控制台)显示“ Max gos”

  2. 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. 有两件事要牢记。

  1. All arguments passed to a method are evaluated prior to the method call. 传递给方法的所有参数都在方法调用之前进行评估。
  2. All methods in Ruby have a return value. Ruby中的所有方法都有一个返回值。 In addition, some methods have side effect, an example of which is to output on the terminal. 此外,某些方法具有副作用,例如在终端上输出。 In any case, returning a value is done after a method has done all its side effects. 无论如何,返回值是在方法完成所有副作用之后完成的。

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方法更改为returnbark方法将没有副作用,并返回"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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM