-2

I am testing small code below

  params[:code] = if false
                                 'N'
                               else
                                 'Y'
                               end

it returns N for if param[:code] passed as true or false also rubucop shows error literal 'false' appeared in a condition. any idea why?

User7354632781
  • 2,174
  • 9
  • 31
  • 54
  • 1
    Possible duplicate of [What does "string literal in condition" mean?](http://stackoverflow.com/questions/21556673/what-does-string-literal-in-condition-mean) – Brad Werth Mar 27 '17 at 21:12

2 Answers2

2

if false

params[:code] = if false
                   'N'
                else
                   'Y'
                end

This code is just :

params[:code] = 'Y'

No if, not but : false is well, always false, so there's no way N can be reached. That's the reason rubocop is complaining.

Modified code

I guess you meant to write :

params[:code] = if params[:code] == false
                   'N'
                else
                   'Y'
                end

It's not rubyish at all, but at least it looks like your code and does what you expect it to.

To test it :

params = {code: false}

params[:code] = if params[:code] == false
                   'N'
                else
                   'Y'
                end
p params
# {:code=>"N"}

Warning!

If your params values are Strings (as they often are), you'll have to test against Strings :

 params[:code] = if params[:code] == "false"
                       'N'
                    else
                       'Y'
                    end

With a ternary :

params[:code] = params[:code] == "false" ? 'N' : 'Y'

Or a hash :

params[:code] = {'true' => 'Y', 'false' => 'N'}[params[:code]]

If you have a fuzzy input (e.g. true or "true" or "True" or "yes"), you could use Rails built-in methods to convert to boolean :

code = ActiveRecord::Type::Boolean.new.type_cast_from_database(params[:code]) # For Rails 4.2
params[:code] = code ? 'Y' : 'N'
Community
  • 1
  • 1
Eric Duminil
  • 52,989
  • 9
  • 71
  • 124
  • thanks. i am trying your modified code but for input value false its assigning param[:code] = Y. are there any other ways? i am using postman for testing http://localhost:3001/D/DB_ID/users/user_id/test?code=false – User7354632781 Mar 27 '17 at 20:56
  • @User7354632781: Your localhost is not my localhost, I cannot access your server. Also, is your input value `"false"` (a String) or `false` (a boolean)? – Eric Duminil Mar 27 '17 at 21:01
  • true or false are boolean – User7354632781 Mar 27 '17 at 21:17
  • They might be strings. `param` values are almost always strings unless you're submitting JSON. – tadman Mar 27 '17 at 21:18
  • Have you considered changing `if params[:code] == false` to `if !params[:code]`, or better still, `unless params[:code]'? Explicit comparison to `false` is not idomatic Ruby. You probably know this, but I'll bet the OP doesn't. – Wayne Conrad Mar 27 '17 at 21:30
  • @WayneConrad: Thanks, I'll update the answer, but I need some more info from OP first. – Eric Duminil Mar 27 '17 at 21:33
  • this one is working code = params[:code] == 'true' ? 'Y' : 'N' – User7354632781 Mar 27 '17 at 21:37
  • @User7354632781 : 'true' is a string, not a boolean. Thanks for the precision. So now, what should be the exact behaviour? What are the possible values for `params[:code]`? – Eric Duminil Mar 27 '17 at 21:40
1

Have you tried this...

params[:code] ? 'Y' : 'N'

Or

params[:code] = params[:code] ? 'Y' : 'N'
# If you are trying to also re-assign params[:code].
SoEzPz
  • 14,958
  • 8
  • 61
  • 64