将全局 $stdout 重新分配给控制台 - ruby

2024-01-18

我试图将 $stdout 设置为临时写入文件,然后再写入文件。

test.rb :
   old_stdout = $stdout    
    $stdout.reopen("mytestfile.out",'w+')
       puts "this goes in mytestfile"
    $stdout= old_stdout
puts "this should be on the console"
    $stdout.reopen("mytestfile1.out",'w+')
       puts "this goes in mytestfile1:"
    $stdout = old_stdout
 puts "this should be back on the console"

这是输出。

ruby test.rb => no output on the console
cat mytestfile.out 
  this goes in mytestfile
  this should be on the console
cat  mytestfile1.out
  this goes in mytestfile1:
  this should be back on the console

我不确定为什么 $stdout 没有重置为控制台?


这个问题可以通过调用来解决dup http://ruby-doc.org/core-1.9.3/Object.html#method-i-dup on $stdout更改之前:

old_stdout = $stdout.dup  
$stdout.reopen("mytestfile.out",'w+')
puts "this goes in mytestfile"
$stdout = old_stdout.dup
puts "this should be on the console"
$stdout.reopen("mytestfile1.out",'w+')
puts "this goes in mytestfile1:"
$stdout = old_stdout
puts "this should be back on the console"

Output:

ruby test.rb
# => this should be on the console
# => this should be back on the console
cat mytestfile.out
# => this goes in mytestfile
cat mytestfile1.out
# => this goes in mytestfile1

以下是我通常将此功能打包到函数中的方式:

# Runs a block of code while blocking stdout.
# Note that /dev/null should be changed to NUL on Windows.
def silence_stdout(log = '/dev/null')
  old = $stdout.dup
  $stdout.reopen(File.new(log, 'w'))
  yield
  $stdout = old
end

Usage:

silence_stdout 'mytestfile.out' do
  puts "this goes in mytestfile"
end

puts "this should be on the console"

silence_stdout 'mytestfile1.out' do
  puts "this goes in mytestfile1"
end

puts "this should be back on the console"

编辑:正如另一张海报提到的,仅在使用纯 Ruby 代码时才需要使用重新打开。上面的函数既适用于纯 Ruby 代码,也适用于使用写入 STDOUT 的 C 扩展等。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将全局 $stdout 重新分配给控制台 - ruby 的相关文章

随机推荐

  • 在 BigQuery 中附加时忽略重复记录

    我们正在将数据从 MySql 写入 BigQuery 我们设定了一些指标 例如 插入 如果是第一次添加记录 则在指示符字段中用 I 保存 更新 如果记录有一些更新的数据 则在 指示符 字段中将其保存为 U 如果未更改则忽略重复记录 但在 更
  • Winnovative - CSS 网格未正确转换

    我正在使用 Winnovative 库将一些 HTML 转换为 PDF 由于这对于很多场景都很有效 但 CSS 网格似乎存在问题 由于我无法分享太多规格 但我可以分享这些显示 HTML 部分和 PDF 中转换结果的图像 HTML eleme
  • 在 WCF 中,我的 UriTemplate 的文字段中可以有通配符吗?

    我正在使用 Net 4 0 编写 RESTful WCF 服务 我想要以下两个网址 root document ids fields fields root externaldocument ids fields fields 映射到相同的
  • applicationContext.xml 中的 beans 声明

    我有一个关于类声明的问题应用程序上下文 xml In 应用程序上下文 xml我们需要指定应用程序中的所有类吗 例如 在我的小型网络应用程序中 我有一个Entity class Service类和DAO班级 所以目前它被定义为
  • 找出 Google Cloud SDK 的安装位置

    我需要在以下位置添加 Google Cloud SDKPATH 所以我需要安装位置的路径 有没有gcloud 哪个命令给我这个信息 如果不是 我必须通过符号链接which gcloud etc 对于这个问题有更清洁的解决方案吗 以下命令将为
  • 有没有办法在html中自动播放音频?

    我想在打开网站时立即在网站中自动播放音频 但这不起作用
  • Python:排序的文件列表

    我使用 os path 从目录生成文件列表 我正在通过 Tkinter 从中生成一个照片库 然而排序是完全随机的 我没有看到目录中显示的照片顺序背后有更大的逻辑 当我打印列表时 它也是随机的 如何更改列表的顺序 按文件名或修改日期从该片段中
  • 具有 Runnable 类的 java 命令模式示例:接收器是否丢失?

    From Java 核心库中 GoF 设计模式的示例 https stackoverflow com questions 1673841 examples of gof design patterns in javas core libra
  • 增加 proxy_send_timeout 和 proxy_read_timeout ingress nginx

    我在 GKE 上运行部署 using quay io kubernetes ingress controller nginx ingress controller 0 12 0图像作为 nginx ingress controller 我正
  • Rscript 使用 renv 环境

    如何使用执行命令RScript myfile R这样它就使用它所在的项目 目录的 renv 环境 而不是我的默认环境 有几种方法 确保您的工作目录设置为您的根目录renv项目 并且 renv 项目的自动加载器处于活动状态 您可以通过调用来设
  • setprop libc.debug.malloc = 1 不起作用

    我尝试使用 setprop libc debug malloc 1 来找出泄漏 我制作了一个演示程序并在其中引入了内存泄漏 但上述标志无法检测到此泄漏 我尝试了以下命令 adb shell setprop libc debug malloc
  • 初始化 CUDA 中的设备数组

    如何初始化使用分配的设备数组cudaMalloc I tried cudaMemset 但它无法初始化除0 code 对于 cudaMemset 如下所示 其中值初始化为 5 cudaMemset devPtr value number b
  • 在方法之间传递变量?

    所以我想为大学编写一个简单的java程序 而我在java方面完全是新手 编译时 我在 printreciept 方法中不断收到错误 错误 找不到符号 我知道这就像无法访问 main 中的变量一样 有人可以帮忙吗 我知道如果我修复它 我可能会
  • 使用嵌入式谷歌表单绕过 CORS

    我正在尝试通过嵌入式表单将表单数据发送到谷歌 我找到了这个post https stackoverflow com questions 40560853 how to use google forms without iframe这似乎回答
  • 使用 PHP 中的 LAME 将 WAV 转换为 MP3

    我有 WAV 数据 想使用 PHP 脚本将其即时转换为 MP3 WAV 文件源自脚本 因此它最初并不是一个文件 我可以运行这样的东西 exec lame cbr b 32k in wav out mp3 但这需要我首先将 in wav 写入
  • 检查事件是否已存在

    我有一个数据网格加载行事件 gridObj LoadingRow new EventHandler
  • Symfony3 JWT 与 LexikJWTAuthenticationBundle 在预检选项上返回 404

    描述 我正在与LexikJWTAuthenticationBundle https github com lexik LexikJWTAuthenticationBundle并尝试使用从我的 Ionic2 应用程序发送的凭据生成 JWT 令
  • 实体框架一对多 TPH 映射

    我使用与此类似的数据结构 其中动物类型是根据表中的鉴别器列确定的 public class Farm public int Id get set public virtual ICollection
  • 在Excel中提取括号之间的数字?

    我一直在寻找这个问题的答案 但没有找到任何可以开始工作的东西 很可能是我对 Excel 的 小程 方面缺乏了解 无论如何 这就是我的情况 在 C2 C17 中 我设置了一些如下文本 2 工作名称 2 表示我们预计作业需要的小时数 其余部分是
  • 将全局 $stdout 重新分配给控制台 - ruby

    我试图将 stdout 设置为临时写入文件 然后再写入文件 test rb old stdout stdout stdout reopen mytestfile out w puts this goes in mytestfile stdo