Rubocop: Can't fix Style/ConditionalAssignment

Created on 10 Aug 2017  路  3Comments  路  Source: rubocop-hq/rubocop

Expected behavior

I have this code:

  if ENV["MEMCACHIER_SERVERS"]
    config.cache_store = :dalli_store,
                        (ENV["MEMCACHIER_SERVERS"] || "").split(","),
                        {:username => ENV["MEMCACHIER_USERNAME"],
                         :password => ENV["MEMCACHIER_PASSWORD"],
                         :failover => true,
                         :socket_timeout => 1.5,
                         :socket_failure_delay => 0.2
                        }
    else
      config.cache_store = :dalli_store
    end

Actual behavior

Rubocop notifies me for a Style/ConditionalAssignment offense. I tried to fix it with:

  config.cache_store = if ENV["MEMCACHIER_SERVERS"]
                         :dalli_store,
                           (ENV["MEMCACHIER_SERVERS"] || "").split(","),
                           {:username => ENV["MEMCACHIER_USERNAME"],
                            :password => ENV["MEMCACHIER_PASSWORD"],
                            :failover => true,
                            :socket_timeout => 1.5,
                            :socket_failure_delay => 0.2
                         }
                       else
                         :dalli_store
                       end

but it can't work like this

/Users/bti/.rvm/gems/ruby-2.4.1/gems/bootsnap-0.3.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:17:in `require': /Users/bti/code/codetriage/config/environments/production.rb:90: syntax error, unexpected ',', expecting keyword_end (SyntaxError)
                 :dalli_store, (ENV["MEMCACHIER_SERVERS"] ||

So for the moment, it's disabled. Is there a way to fix this?

RuboCop version

$ rubocop -V
0.49.1 (using Parser 2.4.0.0, running on ruby 2.4.1 x86_64-darwin16)
bug

All 3 comments

Minimal code to reproduce

if cond
  foo = 1
else
  foo = 1, 2
end

There are two solutions.

First, ignore this case. This cop does nothing for this code.

Second, add offense, and autocorrect with brackets.
For example:

foo= if cond
    1
  else
    [1, 2]
  end

Which is better? I think the second solution is better, but the first solution is not bad either.

Thanks a lot. Will implement the one with the brackets.

BTW love your avatar.

We can not close this issue yet, because this cop is still broken.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

cabello picture cabello  路  3Comments

lepieru picture lepieru  路  3Comments

AndreiMotinga picture AndreiMotinga  路  3Comments

bquorning picture bquorning  路  3Comments

bbatsov picture bbatsov  路  3Comments