Topic: Conditional assignment - an easy Q

Hi, I was wondering whether there was a nice simple one line piece of ruby code to do the following. I'm sure i've read something which does this, or something similar; my mind is a little rusty today!

If a value currently equals A then change it to B, if it currently equals B then change it to A, if it equals neither then set it to B.

I'm not really fussed if it's more than one line, just thought ruby might have something that would do it in one, ;-)


Re: Conditional assignment - an easy Q

Will this work?

if value == "B"
  value = "A"
  value = "B"

The one line version is this:

value = (value == "B" ? "A" : "B")

Railscasts - Free Ruby on Rails Screencasts