Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better use #remove_possible_method here #388

Merged
merged 1 commit into from May 8, 2011

Conversation

smartinez87
Copy link
Contributor

Use #remove_possible_method in favor of #remove_method if method_defined?


class DateTime
# Ruby 1.9 has DateTime#to_time which internally relies on Time. We define our own #to_time which allows
# DateTimes outside the range of what can be created with Time.
remove_method :to_time if instance_methods.include?(:to_time)
remove_possible_method :to_time
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If instance_methods doesn't have the to_time method, will the remove_method throw a NameError like the other cases?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vijaydev I don't follow the question exactly. Although this case is indeed different from the other ones because it will never be true for Ruby 1.8 as instance_methods return strings.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. The question was: When using the remove_possible_method, if the method doesn't exist, remove_method throws a NameError which is quietly ignored. Is that equivalent to checking for the method being part of the instance_methods and then calling remove_method ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. But again, in this particular case, it is only removing the method on 1.9. So remove_possible_method should not be used here.

@smartinez87
Copy link
Contributor Author

Removed the use of #remove_possible_method on DateTime::Conversions

josevalim added a commit that referenced this pull request May 8, 2011
Better use #remove_possible_method here
@josevalim josevalim merged commit 9727dd1 into rails:master May 8, 2011
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.

None yet

3 participants