Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby: toggle a boolean inline?

Tags:

ruby

How can I get the opposite of a boolean in Ruby (I know that it is converted to 0/1) using a method inline?

say I have the given instance:

class Vote

  def return_opposite
    self.value
  end

end

Which obviously doesn't do anything, but I can't seem to find a method that is simple and short something like opposite() or the like. Does something like this exist and I'm just not looking at the right place in the docs? If one doesn't exist is there a really short ternary that would toggle it from 1 => 0 or 0 => 1?

like image 712
JP Silvashy Avatar asked Jan 17 '10 02:01

JP Silvashy


1 Answers

I like to use this

@object.boolean = [email protected]
like image 102
Rodrigo Zurek Avatar answered Oct 16 '22 07:10

Rodrigo Zurek