class RuboCop::Cop::Lint::UselessDefined
defined?(Foo) && Foo.const_defined?(bar) && Foo.const_get(bar).const_defined?(:Baz)
bar = ‘Bar’
# good
defined?(“Foo::#{bar}::Baz”)
bar = ‘Bar’
# bad - interpolation
defined?(foo_bar)
defined?(FooBar)
# good
defined?(‘foo_bar’)
defined?(:foo_bar)
defined?(:FooBar)
defined?(‘FooBar’)
# bad
@example
* ‘binding.local_variable_defined?`
* `instance_variable_defined?`
* `method_defined?`
* `const_defined?`
* `class_variable_defined?`
code with `defined?`. In these cases, switch to one of the more specific methods:
When interpolation is used, oftentimes it is not possible to write the
a `NameError`.
You can safely pass in what you are checking for directly, without encountering
`defined?` is part of the Ruby syntax and doesn’t behave like normal methods.
check for the existence of a constant, method, or variable instead.
Such calls will always return ‘’expression’‘, you probably meant to
Checks for calls to `defined?` with strings or symbols as the argument.
def on_defined?(node)
def on_defined?(node) # NOTE: `defined?` always takes one argument. Anything else is a syntax error. return unless (type = TYPES[node.first_argument.type]) add_offense(node, message: format(MSG, type: type)) end