The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
Stupid extension of Ruby's arithmetic operators

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Florian Frank

Posts: 48
Nickname: ffrank
Registered: Dec, 2005

Florian Frank is a humanoid life-form, living on the third planet of the solar system.
Stupid extension of Ruby's arithmetic operators Posted: Feb 22, 2007 6:20 AM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by Florian Frank.
Original Post: Stupid extension of Ruby's arithmetic operators
Feed Title: The Rubylution: Tag Ruby
Feed URL: http://rubylution.ping.de/xml/rss/tag/ruby/feed.xml
Feed Description: Starts… Now.
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Florian Frank
Latest Posts From The Rubylution: Tag Ruby

Advertisement

Here are the results of this experiment. I tried to extend Ruby's arithmetic operators, starting from

  s = "a" * 3       # => "aaa"

Of course this should be commutative, if possible:

  s = 3 * "a"       # => "aaa"

Division by a string could work like this:

  t = s / ''        # => ["a", "a", "a"]

Coincidently this works also for regular expressions:

  "a.b,c" / /[,.]/ # => ["a", "b", "c"]

Multiplication of an array with a number is already defined in Ruby like this:

  a = t * 2         # => ["a", "a", "a", "a", "a", "a"]

This should commute again:

  a = 2 * t         # => ["a", "a", "a", "a", "a", "a"]

Division by number would be nicely defined by this

  b = a / 3         # => ["a", "a"]

in order to make multiplication work accordingly.

Division would then be like integer division for the number of array elements:

  b = 3 * t / 2     # => ["a", "a", "a", "a"]

In Ruby Array#* is already taken:

  c = a * ', '      # => "a, a, a, a, a, a"

To make it commute, this can be added:

  c = ', ' * a      # => "a, a, a, a, a, a"

Ok, we already handled this above:

  d = c / ', '      # => ["a", "a", "a", "a", "a", "a"]

Now subtraction of strings could be defined like this:

  e = c - ", "      # => "aa, a, a, a, a"
  e -= ", "         # => "aaa, a, a, a"
  e -= ", "         # => "aaaa, a, a"

And addition is already taken by Ruby:

  e += "bb"         # => "aaaa, a, abb"

Let's continue subtracting:

  e -= ", "         # => "aaaaa, abb"
  e -= ", "         # => "aaaaaabb"

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:

  f = "abc" ** 0    # => "a"

Power of 1 should be the identity:

  f = "abc" ** 1    # => "abc"

And power of two should have power of two of the original length:

  f = "abc" ** 2    # => "abcabcabc"

I am glad, that there is no operator for roots in Ruby, so let's skip them.

This is true for (some) numbers:

  g = 6             # => 6
  (g / 2) * 2 == g  # => true

So this should be true, too:

  g = "a" * 6       # => "aaaaaa"
  (g / 2) * 2 == g  # => true

Divison of numbers by strings is then strangely defined like multiplication:

  h = "abc"         # => "abc"
  3 / h             # => "abcabcabc"

And multiplication of two strings, yields the number of non-overlapping occurrences of the second string in the first, to make this true again:

  (3 / h) * h == 3  # => true

Ok, now let's divide numbers by arrays:

  i = %w[a b c]     # => ["a", "b", "c"]
  3 / i             # => ["a", "b", "c", "a", "b", "c", "a", "b", "c"]

It has to be the equal to 3 * i or i * 3, similar to 3 / h (h being a string) from above.

  (3 / i) * i == 3  # => true

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

Topic: Sudoku solver Previous Topic   Next Topic Topic: Must Java Have an Answer to Rails?

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use