The question is: Can I define my own custom operator in Ruby, except for the ones found in
“Operator Expressions“?
For example: 1 %! 2
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Yes, custom operators can be created, although there are some caveats. Ruby itself doesn’t directly support it, but the superators gem does a clever trick where it chains operators together. This allows you to create your own operators, with a few limitations:
Then:
Due to the aforementioned limitations, I couldn’t do your
1 %! 2example. The Documentation has full details, but Fixnums can’t be given a superator, and!can’t be in a superator.