在OpsCode Wiki中有以下文档:
require 'ohai'
# ...
# Profit! ;-)
如何使用irb打印“ohai”命令提供的json数据?我试图查看application.rb中的代码,但得到的数据为空。
require 'ohai/application'
ohai = Ohai::System.new
ohai.json_pretty_print
=> "{\n\n}"
我不想在Chef(或Shef)中这样做,我只想在我自己的应用程序中使用ohai gem本身。
最佳答案
在Ohai::Application
类(运行ohai
时得到的结果)中插入,#run_application
将实例化Ohai::System
,除非它是由文件配置的,否则它将调用all_plugins
来填充数据。
假设,Ohai::System#all_plugins
将数据收集委托给the lib/ohai/plugins
directory。
$ irb -rohai
> system = Ohai::System.new
=> #<Ohai::System:0x00000100988950 @data={}, @seen_plugins={}, @providers={}, @plugin_path="", @hints={}>
> system.all_plugins
=> true
> puts system.to_json
{"languages":{"ruby":{"platform":"x86_64-darwin10.8.0","version":"1.9.2", ...
> system.to_json.size
=> 42395