Ruby redefine "and" keyword in class?

Is it possible to redefine keywords like and in ruby? I have some DSL code like this:

rule condition and another_condition do
  do_something
end

I would like and to just be another ruby method inside my class just like condition and another_condition. Is this possible?

Answers


Keywords like like and and or cannot be overloaded (overridden).

This article explains operator overloading and specifies what operators cannot be overridden:

http://strugglingwithruby.blogspot.com/2010/04/operator-overloading.html

At the end of the article it states:

You cannot override keywords, such as "or", nor can you override:
&& & || | () {} :: . ~ .. ...

Update

As Jörg W Mittag pointed out, that's wrong. and, or, && and || are the only operators that cannot be overridden. not can be overridden via !.


It is not supported by language.

But you can always try utils like this one.


Need Your Help

BackgroundWorker limit number of workers and create them as become available

c# multithreading backgroundworker

I have a function that is creating a BackgroundWorker for each object in the list, what I want to do now is limit the number of workers created to 20, and as they finished, process the next item in...

How to get each device's IP address in Wi-Fi Direct scenario?

android wifi android-wifi wifimanager wifi-direct

Starting from ICS, Wi-Fi Direct is introduced. Normally, we use the WifiP2pManager class to operate on Wi-Fi Direct, but it seems that it can only retrieve the GroupOwner IP address after connected...