0

クラスの1つがsthに似ている宝石を持っています:

class Test
    TESTING = {
        :sth1 => 'foo',
        :sth2 => 'bar'
    }

    # p Test.new.show 
    # should print 'cat'
    def show
        p TESTING[:sth3]
    end 

end

他のファイルで拡張しました

# in other file
class Test 
    TESTING = {
        :sth3 => 'cat'
    }       
end

しかし、コードの最初の部分が立っているので、最初のファイルで :sth3 を使用する必要があります。事前にt​​hx。

4

1 に答える 1

2

拡張しませんでした。ハッシュを新しいものに置き換えました。修正方法は次のとおりです。

# in the other file
Test::TESTING[:sth3] = 'cat'

割り当てを任意の順序で配置できるように、遅延初期化を伴うメソッドを使用することをお勧めします。

class Test
  def self.testing
    @testing ||= {}
  end

  testing[:sth1] = 'foo'
  testing[:sth2] = 'bar'
end

# in the other file
Test.testing[:sth3] = 'cat'
于 2012-07-03T14:52:36.227 に答える