ruby - Understanding Rubocop "code unreachable" cop -



ruby - Understanding Rubocop "code unreachable" cop -

i have next statement

if !defined?(proxy) || proxy.blank?

and trying accomplish avoiding undefined methods on nil object. intention

check if "proxy" object defined begin with check if "proxy" object not blank (but otherwise defined)

rubocop says "unreachable code detected" but, read it, object "proxy" can defined empty means sec part of if statement evaluated.

how code unreachable?

edit based on our discussion in chat:

defined? not right approach, check whether variable not defined, not value is. operates on meta-level, speak. think of defined? keyword answering question "was thing declared anywhere in code before?".

normally, check nil value using object#nil? method. way inquire question "is variable empty?". code declares proxy variable in case, reason why code within if-statement unreachable: proxy never undefined.

you can write

if proxy.nil? || proxy.blank? # ... end

ruby rubocop

Comments

Popular posts from this blog

model view controller - MVC Rails Planning -

ruby on rails - Devise Logout Error in RoR -

html - Submenu setup with jquery and effect 'fold' -