Closed gjtorikian closed 7 years ago
@laertispappas suggestions?
@gjtorikian Thanks for the report.
default
or any literal that starts with lower case is not a valid constant in Ruby since it needs to start with an upper case letter. After replacing const_missing
with const_set
we no longer support lower case enum names.
We can address the problem by rescuing the NameError
exception and defining an Enum method like this:
def define (key, value)
# ...
const_set key, value
rescue NameError
define_singleton_method(key) { value }
end
@dblock With the above Parse::default
or Parse.default
can be accessed. I don't know how clear this solution is though since our current implementation use constants as it's internal structure. I think we should force users to use constants when defining enums and raise a meaningful error message if he tries to define an enum with the first char in lowercase.
Didn't realize that this would become a limitation. Maybe we can support both with a Ruby::Enum::Const
and Ruby::Enum::Set
, then alias the latter to Ruby::Enum
?
@dblock I don't understand what you are proposing. Can you give an example?
Right now we have a single implementation, Ruby::Enum
, but we could very well have two that define
in different ways, one with const_set
and another with the old way. So someone who wants to use lowercase symbols could still use the old style module.
Agree with you it's the implementation tha concerns me. If no one is working on this I will fork a branch to prepare a fix.
Fixes in #18 . Released on v0.7.2
Given a Gemfile like:
And a Rakefile like:
Running
rake go
bombs on 0.7.1:Versions 0.7.0 and lower worked as expected, though.