Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 有没有办法让你的评论在你把它们放进输出文件后显示出来?_Ruby_Command Line_Comments - Fatal编程技术网

Ruby 有没有办法让你的评论在你把它们放进输出文件后显示出来?

Ruby 有没有办法让你的评论在你把它们放进输出文件后显示出来?,ruby,command-line,comments,Ruby,Command Line,Comments,在我的Ruby文件中,我有多个注释,我正在将输出保存到一个文本文件中,我想知道是否有任何方法可以使注释显示在我的新输出文件中。mysql有一种方法可以做到这一点,它在命令行中使用--comments。我想知道Ruby是否有类似的东西 例如,我希望我的姓名和实验室名称显示在lab1.txt文件中: 我的Ruby代码: #My name #lab1.rb #description #part 1 puts "hello world!" #part 2 puts "hello world".len

在我的Ruby文件中,我有多个注释,我正在将输出保存到一个文本文件中,我想知道是否有任何方法可以使注释显示在我的新输出文件中。mysql有一种方法可以做到这一点,它在命令行中使用
--comments
。我想知道Ruby是否有类似的东西

例如,我希望我的姓名和实验室名称显示在
lab1.txt
文件中:

我的Ruby代码:

#My name
#lab1.rb
#description

#part 1
puts "hello world!"

#part 2
puts "hello world".length
将输出保存到另一个文件:

ruby lab1.rb > lab1.txt
重定向标准输出时添加行 Ruby解释器忽略注释。虽然您不能让解释器自动打印注释,但您当然可以添加代码,仅当您的代码具有重定向的标准输出时(例如,将输出传输到文件时),才会打印附加信息。例如:

if ! $stdout.tty?
  puts 'Name: John Doe'
  puts 'Lab:  lab1'
  puts
end

puts 'Hello, world!'
puts 'Hello, world!'.length
在不重定向的情况下运行时,您将只看到最后两行的输出:

$ ruby redirection.rb 
Hello, world!
13
重定向到文件或管道时,if语句中的代码也将被重定向:

$ ruby redirection.rb | tee /tmp/lab1.txt
Name: John Doe
Lab:  lab1

Hello, world!
13

根据定义,解释器忽略注释。不,没有。不要对此使用注释,请使用
put