Miguel de Sousa
Miguel de Sousa

Reputation: 344

ternary works different in local and heroku

I have this method it's very simple and almost all of the time the isTrue param should be false and return "2".

def test(isTrue = false)
  isTrue ? 1 : 2
end

this works fine in my dev env but when I push it to heroku suddenly it starts returning as if it is true, and Im absolutly positive that its false. I think it possibly be checking if the var is nil ( or something like that)

I changed the ternary to:

isTrue == true ? 1 : 2

And it corrects the problem, I don't understand why this happens. Can someone explain it? thanks!

Upvotes: 0

Views: 61

Answers (2)

dax
dax

Reputation: 10997

isTrue = true ? 1 : 2 

this will always return 1 as Mark Meeus commented.

= is the assignment operator in ruby, used to make assign a variable a given value.

==, however is a comparison operator.

so with your code as it is currently, you're assigning "isTrue = true" and then telling the code to return 1 if isTrue is true.

Upvotes: 0

zwippie
zwippie

Reputation: 15515

This is definitely wrong:

isTrue = true ? 1 : 2

It sets the variable isTrue to true and uses the result of that statement (true) as the input of the ternary operator, so this will always return true.

Change it to:

isTrue == true ? 1 : 2

Regarding the differences between development and production mode: check that you really feed booleans into the method and not integers (0 or 1), strings ('0', '1', 't', 'f', 'y', 'n', etc) or nil.

Upvotes: 1

Related Questions