Created
June 21, 2012 15:54
-
-
Save carlosantoniodasilva/2966638 to your computer and use it in GitHub Desktop.
Class Attribute test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'active_support/all' | |
class A | |
class_attribute :attrs | |
self.attrs = {} | |
end | |
class B < A | |
end | |
class C < A | |
end | |
def puts_attrs(desc) | |
puts desc | |
puts A.attrs | |
puts B.attrs | |
puts C.attrs | |
end | |
puts_attrs 'Unset' | |
A.attrs[:a] = 1 | |
puts_attrs 'A []=' | |
A.attrs.merge!(b: 2) | |
puts_attrs 'A merge!' | |
A.attrs = A.attrs.merge(c: 3) | |
puts_attrs 'A =merge' | |
B.attrs[:x] = 1 | |
puts_attrs 'B []=' | |
B.attrs.merge!(y: 2) | |
puts_attrs 'B merge!' | |
B.attrs = B.attrs.merge(z: 3) | |
puts_attrs 'B = merge' | |
=begin # output | |
$ ruby class_attribute_test.rb | |
Unset | |
{} | |
{} | |
{} | |
A []= | |
{:a=>1} | |
{:a=>1} | |
{:a=>1} | |
A merge! | |
{:a=>1, :b=>2} | |
{:a=>1, :b=>2} | |
{:a=>1, :b=>2} | |
A =merge | |
{:a=>1, :b=>2, :c=>3} | |
{:a=>1, :b=>2, :c=>3} | |
{:a=>1, :b=>2, :c=>3} | |
B []= | |
{:a=>1, :b=>2, :c=>3, :x=>1} | |
{:a=>1, :b=>2, :c=>3, :x=>1} | |
{:a=>1, :b=>2, :c=>3, :x=>1} | |
B merge! | |
{:a=>1, :b=>2, :c=>3, :x=>1, :y=>2} | |
{:a=>1, :b=>2, :c=>3, :x=>1, :y=>2} | |
{:a=>1, :b=>2, :c=>3, :x=>1, :y=>2} | |
B = merge | |
{:a=>1, :b=>2, :c=>3, :x=>1, :y=>2} | |
{:a=>1, :b=>2, :c=>3, :x=>1, :y=>2, :z=>3} | |
{:a=>1, :b=>2, :c=>3, :x=>1, :y=>2} | |
=end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment