Caret(^) symbol in Ruby & Ruby on Rails

gautamsawhney

Gautam Sawhney

Posted on March 18, 2020

Caret(^) symbol in Ruby & Ruby on Rails

^ symbol has multiple uses in ROR.

1- As a bitwise XOR Operator

For each bit in the binary representation of the operands, a bitwise XOR will get a 1 bit if one of the corresponding bits in the operands is 1, but not both, otherwise, the XOR will get a 0 bit. Here's an example:

5     = 101
6     = 110
5 ^ 6 = 011 = 3

2- In Rails validation error message

When you want to override the error message without the attribute name, simply prepend the message with ^ like

validates_numericality_of :identity_id, :message => "^Person field definition id must be an integer"

you get

Person field definition id must be an integer

instead of

Identity Id Person field definition id must be an integer

💖 💪 🙅 🚩
gautamsawhney
Gautam Sawhney

Posted on March 18, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related