Skip to content

Remove const_missing method in favor of const_set - #11

Closed
dmoss18 wants to merge 1 commit into
dblock:masterfrom
dmoss18:remove-const-missing
Closed

dmoss18 wants to merge 1 commit into
dblock:masterfrom
dmoss18:remove-const-missing

Conversation

@dmoss18

@dmoss18 dmoss18 commented Oct 27, 2016

Copy link
Copy Markdown

No description provided.

@dblock

dblock commented Oct 28, 2016

Copy link
Copy Markdown
Owner

Interesting. Could you provide a longer explanation please?

This changes behavior and API. Needs tests, CHANGELOG, UPGRADING, a fixed build, etc.

@dmoss18

dmoss18 commented Nov 2, 2016

Copy link
Copy Markdown
Author

Longer explanation is method_missing can hijack constants at a higher level. For example:

class MyEnum
  include Ruby::Enum
  define MY_GOOD_CONST, 'good'
end

MY_HIJACKED_CONST = 'hijacked'.freeze
$> MyEnum::MY_GOOD_CONST // good
$> MyEnum::MY_HIJACKED_CONST // hijacked <-- this is bad

Now it appears that my enum contains the const that is defined outside the MyEnum class. This is unexpected and erroneous behavior.

Also, using const_set will raise a NameError instead of the custom error you defined, which is more ideal.

Tests and changelog to follow

@dblock

dblock commented Nov 2, 2016

Copy link
Copy Markdown
Owner

How interesting, thanks!

@dmoss18

dmoss18 commented Nov 2, 2016

Copy link
Copy Markdown
Author

The real risk is having one of your enums get re-defined somewhere else:

class MyEnum
  include Ruby::Enum
  define MY_GOOD_CONST, 'good'
end

MY_GOOD_CONST = 'bad'.freeze
$> MyEnum::MY_GOOD_CONST //bad

@dblock

dblock commented Dec 5, 2016

Copy link
Copy Markdown
Owner

@dmoss18 Can you see ^^^ please, would like to get this merged.

@dblock

dblock commented Feb 19, 2017

Copy link
Copy Markdown
Owner

Bump @dmoss18

@laertispappas

laertispappas commented Feb 21, 2017 •

Copy link
Copy Markdown
Collaborator

@dmoss18 Will you fix this PR? I'm about to implement this if it's OK from you.

@dblock

dblock commented Feb 21, 2017

Copy link
Copy Markdown
Owner

@laertispappas you should finish it, haven't heard from @dmoss18 for a while

laertispappas added a commit to laertispappas/ruby-enum that referenced this pull request Feb 22, 2017
Define constant with const_set instead of "fetching"
them with const_missing. These changes were proposed
by @dmoss18 in his initial PR (dblock#11).

I also add a spec to support the new feature. I kept
const_missing method in order to raise the custom
Ruby::Enum::Errors::DuplicateKeyError since
it provides a handy way to inform the end user
for the exception raised.
laertispappas added a commit to laertispappas/ruby-enum that referenced this pull request Feb 22, 2017
Define constant with const_set instead of "fetching"
them with const_missing. These changes were proposed
by @dmoss18 in his initial PR (dblock#11).

I also add a spec to support the new feature. I kept
const_missing method in order to raise the custom
Ruby::Enum::Errors::DuplicateKeyError since
it provides a handy way to inform the end user
for the exception raised.
@laertispappas

Copy link
Copy Markdown
Collaborator

I'm gonna close this one since it's being addressed in #16

laertispappas added a commit to laertispappas/ruby-enum that referenced this pull request Feb 22, 2017
Define constant with const_set instead of "fetching"
them with const_missing. These changes were proposed
by @dmoss18 in his initial PR (dblock#11).

I also add a spec to support the new feature. I kept
const_missing method in order to raise the custom
Ruby::Enum::Errors::DuplicateKeyError since
it provides a handy way to inform the end user
for the exception raised.
laertispappas added a commit to laertispappas/ruby-enum that referenced this pull request Feb 22, 2017
Define constant with const_set instead of "fetching"
them with const_missing. These changes were proposed
by @dmoss18 in his initial PR (dblock#11).

I also add a spec to support the new feature. I kept
const_missing method in order to raise the custom
Ruby::Enum::Errors::DuplicateKeyError since
it provides a handy way to inform the end user
for the exception raised.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants