-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
Allow self.Foo #628
Allow self.Foo #628
Conversation
Coverage decreased (-0.1%) when pulling 6fe373355da502b689b6d1229a6d09832125e2e8 on chulkilee:allow-self into 62a7059 on bbatsov:master. |
I'm not sure I understand the nature of this problem. If the constant was defined at this point it would surely be accessible without |
@bbatsov I think it's this case. You need class Klass
def A
puts 'A'
self.B
end
def B
puts 'B'
end
end
Klass.new.A |
Coverage decreased (-0.1%) when pulling b89939eb9d6417910f5af46fbbdd15446e33c33a on chulkilee:allow-self into dc7544f on bbatsov:master. |
Foo = 3
class Klass
def method_missing(meth, *args, &block)
"missing #{meth}"
end
def test
self.Foo
end
def test_constant
Foo
end
end
puts Klass.new.test
puts Klass.new.test_constant |
You can also do
|
First, self.Foo and Foo is different, so self.Foo should not be considered as "redundant" - it's rubocop bug. Second, as @agrimm mentioned, that I think calling self.Foo sounds more like attribute than Foo() so there are some cases self.Foo is more natural. See the following example. class Original
def Id
"fake id"
end
end
class Wrapper
def initialize(raw)
@raw = raw
end
def details
"#{self.Id}"
end
def method_missing(meth, *args, &block)
@raw.send(meth)
end
end
original = Original.new
wrapper = Wrapper.new(original)
puts wrapper.details Wrapper#Id is more like attribute rather than method - which can be done by omitting parenthesis (which is absolutely legit in ruby) This is the case when I need to wrap a restfoce object. restforce uses hashie to wrap Salesforce response, which includes camel case attributes (like Id, Name etc). If we want to add rules to discourage using camel case method name, that should be another cop, not in this RedundantSelf cop. |
omitting `self` in `self.Foo` gives uninitialized constant NameError
@jonas054 @yujinakayama I'm not sure what's the best way to handle this, since CamelCase methods are quite rare and generally used only for conversions. Maybe the best option would be to implement this as an option - |
Since both |
Sounds reasonable. |
omitting
self
inself.Foo
gives uninitialized constant NameError