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 !.


Need Your Help

Xcode build failed “Permission denied” when using phone gap .sh files

xcode permissions

When I get a phonegap project in xcode ,I failed to build it with a permission issue in .sh file...Is there any help?

Jquery UI Mobile returning empty value for form

jquery forms search mobile

I cannot seem to figure this out. You can view an example of what I've done so far here: http://jsfiddle.net/aCUQC/1/

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.