dry-rb / dry-container

A simple, configurable object container implemented in Ruby
https://dry-rb.org/gems/dry-container
MIT License
335 stars 41 forks source link

Strings and Symbols #8

Closed sagmor closed 8 years ago

sagmor commented 8 years ago

I believe it would make sense to cast keys into strings

Right now the following happens if you switch between Symbols and Strings:

container = Dry::Container.new
container.register(:foo, 'bar')
container[:foo]
# => "bar"
container['foo']
# => Dry::Container::Error: Nothing registered with the key "foo"

The confusion appears when you start to namespace things, for example:

container = Dry::Container.new
container.namespace(:test) do
  register(:foo, 'bar')
end

# Now it's a string
container['test.foo']
# => "bar"

I think it would be clearer if things got stored as strings by default. If you think that's better I can push a PR with the change.

solnic commented 8 years ago

I agree, consistency FTW