Skip to content

Instantly share code, notes, and snippets.

@mehdi-farsi
Last active April 16, 2018 17:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mehdi-farsi/e792980a02c1f5cd2cf6de28d218358f to your computer and use it in GitHub Desktop.
Save mehdi-farsi/e792980a02c1f5cd2cf6de28d218358f to your computer and use it in GitHub Desktop.
Unary Operators for Non-Numeric Objects

Unary Operators for Non-Numeric Objects

It's possible to implement unary operators to an object by defining the -@ and +@ methods within the class declaration

class True
  def -@
    false
  end
  
  def +@
    true
   end
end

t = True.new

p -t
p +t

produces

false
true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment