Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Use method_defined? instead of responds_to? #282

Merged
merged 1 commit into from

2 participants

@aamerabbas

The string extension for "camelize" checks if this method has already been defined by called responds_to?. This, however, doesn't work because it's being called on the String class rather than an instance of the String class (and hence, will always return false).

This means that if String#camelize is already defined, it will always be overridden. One notable situation where this breaks things is when you call something like 'some_string'.camelize(:lower) since the ActiveSupport version of camelize can accept an optional argument whereas the one defined in the Twitter gem cannot.

@aamerabbas aamerabbas Use method_defined? instead of responds_to?
The string extension for "camelize" checks if this method has already been defined by called responds_to?. This, however, doesn't work because it's being called on the String class rather than an instance of the String class (and hence, will always return false).

This means that if String#camelize is already defined, it will always be overridden. One notable situation where this breaks things is when you call something like 'some_string'.camelize(:lower) since the ActiveSupport version of camelize can accept an optional argument whereas the one defined in the Twitter gem cannot.
79a4d0b
@sferik sferik merged commit 47d599c into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jul 7, 2012
  1. @aamerabbas

    Use method_defined? instead of responds_to?

    aamerabbas authored
    The string extension for "camelize" checks if this method has already been defined by called responds_to?. This, however, doesn't work because it's being called on the String class rather than an instance of the String class (and hence, will always return false).
    
    This means that if String#camelize is already defined, it will always be overridden. One notable situation where this breaks things is when you call something like 'some_string'.camelize(:lower) since the ActiveSupport version of camelize can accept an optional argument whereas the one defined in the Twitter gem cannot.
This page is out of date. Refresh to see the latest.
Showing with 1 addition and 1 deletion.
  1. +1 −1  lib/twitter/core_ext/string.rb
View
2  lib/twitter/core_ext/string.rb
@@ -5,6 +5,6 @@ class String
# @return [String]
def camelize
self.gsub(/\/(.?)/){"::#{$1.upcase}"}.gsub(/(?:^|_)(.)/){$1.upcase}
- end unless respond_to?(:camelize)
+ end unless method_defined?(:camelize)
end
Something went wrong with that request. Please try again.