|
Advertisement
|
Here are the results of this experiment. I tried to extend Ruby's arithmetic operators, starting from
Of course this should be commutative, if possible:
Division by a string could work like this:
Coincidently this works also for regular expressions:
Multiplication of an array with a number is already defined in Ruby like this:
This should commute again:
Division by number would be nicely defined by this
in order to make multiplication work accordingly.
Division would then be like integer division for the number of array elements:
In Ruby Array#* is already taken:
To make it commute, this can be added:
Ok, we already handled this above:
Now subtraction of strings could be defined like this:
e = c - ", "
e -= ", "
e -= ", "
And addition is already taken by Ruby:
Let's continue subtracting:
Ok, let's power a bit. Of course power of 0 should be the one element, so let's create a string of length 1:
Power of 1 should be the identity:
And power of two should have power of two of the original length:
I am glad, that there is no operator for roots in Ruby, so let's skip them.
This is true for (some) numbers:
So this should be true, too:
g = "a" * 6
(g / 2) * 2 == g
Divison of numbers by strings is then strangely defined like multiplication:
And multiplication of two strings, yields the number of non-overlapping occurrences of the second string in the first, to make this true again:
Ok, now let's divide numbers by arrays:
It has to be the equal to 3 * i or i * 3, similar to 3 / h (h being a string) from above.
The 3 / something bit was a kind of a surprise to me, but it seems to result from the operator choices that were already made in Ruby and by me.
Other than that, I already said, that it was a stupid experiment in title of this blog post. ;) I hope you enjoyed it anyway...
Read: Stupid extension of Ruby's arithmetic operators