diff options
author | Noah Kantrowitz <noah@coderanger.net> | 2017-04-03 13:15:26 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-04-03 13:15:26 -0500 |
commit | 9413e9ab1f703129204a6f4eafc0e307d5aa8eb7 (patch) | |
tree | 6f193418bb84b5192d6c59128a58af0f7fce8b55 | |
parent | 96ac18a2cfa8616ae6d4fb1839554aff9ad2938a (diff) | |
parent | 7e39dfa20504c96466484d634e97595456fc37b9 (diff) | |
download | ohai-9413e9ab1f703129204a6f4eafc0e307d5aa8eb7.tar.gz |
Merge pull request #972 from coderanger/freeze-strings
Freeze all string values coming out of Ohai.
-rw-r--r-- | lib/ohai/system.rb | 23 | ||||
-rw-r--r-- | spec/unit/system_spec.rb | 4 |
2 files changed, 27 insertions, 0 deletions
diff --git a/lib/ohai/system.rb b/lib/ohai/system.rb index e66ba90b..56957f01 100644 --- a/lib/ohai/system.rb +++ b/lib/ohai/system.rb @@ -107,6 +107,9 @@ module Ohai Ohai::Log.error("Encountered error while running plugins: #{e.inspect}") raise end + + # Freeze all strings. + freeze_strings! end def have_v6_plugin?(name) @@ -231,5 +234,25 @@ module Ohai Ohai::Log.level = Ohai.config[:log_level] end end + + # Freeze all string values in @data. This makes them immutable and saves + # a bit of RAM. + # + # @api private + # @return [void] + def freeze_strings! + # Recursive visitor pattern helper. + visitor = lambda do |val| + case val + when Hash + val.each_value { |v| visitor.call(v) } + when Array + val.each { |v| visitor.call(v) } + when String + val.freeze + end + end + visitor.call(@data) + end end end diff --git a/spec/unit/system_spec.rb b/spec/unit/system_spec.rb index 3bfafcf1..0941b1f0 100644 --- a/spec/unit/system_spec.rb +++ b/spec/unit/system_spec.rb @@ -166,6 +166,8 @@ EOF ohai.all_plugins expect(ohai.data[:zoo]).to eq("animals") expect(ohai.data[:park]).to eq("plants") + expect(ohai.data[:zoo]).to be_frozen + expect(ohai.data[:park]).to be_frozen end describe "when using :disabled_plugins" do @@ -312,6 +314,8 @@ EOF ohai.all_plugins expect(ohai.data[:zoo]).to eq("animals") expect(ohai.data[:park]).to eq("plants") + expect(ohai.data[:zoo]).to be_frozen + expect(ohai.data[:park]).to be_frozen end it "should write an error to Ohai::Log" do |