From 88979eb39bc4bcd8f104848b5d9e34b7f77daa08 Mon Sep 17 00:00:00 2001 From: Kouhei Sutou Date: Sun, 5 Oct 2014 15:53:44 +0900 Subject: [PATCH] Suppress "already initialized constant KEYS" warning `Class.new {...}` uses a closure to define a class. Closure keeps environment that includes local variables and so on. Assignment changes the environment. So `klass = Class.new { KEYS = [] }` doesn't define `klass::KEYS`. It defines `Object::KEYS`. a = 1 Class.new do a = 2 X = 3 end p a # => 2 p X # => 3 We need to use `const_set` to define a constant in `Class.new {...}`. --- spec/models/fluentd/setting/common_spec.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/models/fluentd/setting/common_spec.rb b/spec/models/fluentd/setting/common_spec.rb index de3c1c4..09f8eaf 100644 --- a/spec/models/fluentd/setting/common_spec.rb +++ b/spec/models/fluentd/setting/common_spec.rb @@ -121,8 +121,8 @@ describe Fluentd::Setting::Common do end @klass = Class.new do include Fluentd::Setting::Common - KEYS = [:key1, :key2, :flag1, :hide, :ch, :child, :string] # FIXME: display "warning: already initialized constant KEYS", but works :( - attr_accessor(*KEYS) + const_set(:KEYS, [:key1, :key2, :flag1, :hide, :ch, :child, :string]) + attr_accessor(*const_get(:KEYS)) booleans :key1, :key2 flags :flag1 hidden :hide