为什么我用这个网站写代码输出的是'=> nil',用Sublime Text却没有?

Why does '=> nil' appear in the output when I use this website to code but not when I use Sublime Text?

我是初学者 Ruby 我注意到当我使用这个网站 (https://repl.it/) 进行编码时,'=> nil' 有时会出现在输出中。但是,当我使用 Sublime Text 编码时,它根本没有出现。

1) 它出现在输出中重要吗?如果是这样,为什么?

2) 如何让它出现在 Sublime Text 中?

谢谢!

nil 在这种情况下,只是您的方法调用的 return 值。它没有 return 任何东西。 repl 会为您打印出来,但是当您 运行 一个 Ruby 文件时,您将看不到它。

例如在 pry、irb、repl.it 等

puts 5

打印

5
=> nil

puts 命令打印 5,然后 returns nil。 repl 会为您打印出来,因此您知道 return 的值是多少。你可以自己试试

def test
  puts 'test'
  return 5
end
test

打印

test
=> 5

如果你想让它在运行创建一个文件时出现,你可以打印函数的return。例如

puts "=> #{test.inspect}"

结果

test
=> 5
=> nil # this line only if running in repl