Skip to content

Commit 7c65416

Browse files
author
Sven Fuchs
committed
symbolize key on assignment
1 parent d801b81 commit 7c65416

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

lib/hashr.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def [](key)
4646
end
4747

4848
def []=(key, value)
49-
super(key, value.is_a?(Hash) ? self.class.new(value, {}) : value)
49+
super(key.to_sym, value.is_a?(Hash) ? self.class.new(value, {}) : value)
5050
end
5151

5252
def respond_to?(name)

test/hashr_test.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,18 @@ def teardown
7777
assert_equal 'foo', hashr.foo
7878
end
7979

80+
test 'method using a string key works' do
81+
hashr = Hashr.new
82+
hashr['foo'] = 'foo'
83+
assert_equal 'foo', hashr.foo
84+
end
85+
86+
test 'using a symbol key works' do
87+
hashr = Hashr.new
88+
hashr[:foo] = 'foo'
89+
assert_equal 'foo', hashr.foo
90+
end
91+
8092
test 'defining defaults' do
8193
klass = Class.new(Hashr) do
8294
define :foo => 'foo', :bar => { :baz => 'baz' }

0 commit comments

Comments
 (0)