[Rake-devel] Overloading the divide symbol on String to facilitate path concatenation?

Michael Bishop mbishop at me.com
Mon Apr 30 19:00:12 UTC 2012


Hello fellow rake developers,

I started using this construction in my rake files and I wanted to get your opinion on it (though if there is a better forum, please direct me there).


Problem
-------

In my rake scripts, I like to assign directory paths to variables so I can use the variables later in my program. The problem is, when concatenating the variables, I end up with ugly combinations like:

  File.join(BUILD_D, "bin", "x86")
  File.join(CACHE_D, "scripts", SCRIPT_TEMP_NAME)


Possible Solution
-----------------

I find this hard to read so I tried something:

  class String
  
    def /(path)
      if !path
        self
      else
        File.join(self, path)
      end
    end
  
  end

Now I can make paths like:

  BUILD_D/"bin"/"x86"
  CACHE_D/"scripts"/SCRIPT_TEMP_NAME


Discussion
----------

My spidey sense is on overload from former C++ days where I was filled with "operator overloading is bad. Always. Never ever do it. Ever... ever"

But I have to say, the more I use this, the more I like it. I can't think of a situation where the operator overloading will get me in to trouble but maybe I haven't thought it through hard enough.

What do you think? Do you have strategies for making path concatenation less ugly in your scripts?

_ michael







More information about the Rake-devel mailing list