Sajjad Sajjad - 6 months ago 11
Ruby Question

Changing the value of a variable between two values

In ruby, is there any method for changing the value of a variable between two values based on its current value?
I need something like this:

var = 'value one'
var.flip('value one', 'value two')
puts var # => 'value two'


So when I call
.flip
it changes the value of
var
to
'value two'
, because the current value of
var
is
'value one'

Answer

I think that you can use the ruby ternary operator or the if - else contruction like this

var1 = 2

var1 = 3 == var1 ? var1 : 2

or

irb(main):049:0> var1 = if 4 == var1 then var1 else 2 end
=> 4
i