Robert Klemme schrieb (am 1.9.10 11:54):
> On Wed, Sep 1, 2010 at 11:39 AM, Philipp Kempgen <lists / kempgen.net> wrote:
>> Robert Klemme wrote:
>>> 2010/8/31 Philipp Kempgen <lists / kempgen.net>:
>>>> Robert Klemme wrote:
>>>>> 2010/8/31 Philipp Kempgen <lists / kempgen.net>:
>>
>>>>>> Actually one of the few nice things about namespaces in PHP is the
>>>>>> ability to import namespaces/classes using an alias.
>>>>>>
>>>>>> use \foovendor\system\Shell as Sh;
>>>>>> Sh::exec( 'ls' );
>>>>>
>>>>> Well, you can do the same in Ruby.  Either use a constant or a local variable:
>>>>>
>>>>> Sh = ::FooVendor::System::Shell
>>>>> Sh.exec 'ls'
>>>>>
>>>>> sh = ::FooVendor::System::Shell
>>>>> sh.exec 'ls'
>>>>
>>>> Sure.
>>>> I guess I'll do some benchmarks. :-)
>>>
>>> I don't expect much differences.  Please share the outcome of the benchmark.
>>
>> In my totally unscientific benchmarks I did not find any noticeable
>> divergence. It is certainly well below 1 %. Fine.
>>
>> ruby 1.9.2p0 (2010-08-18 revision 29036) [i686-linux]
> 
> Thanks for sharing your findings!  To make it "scientific" you could
> also post the benchmark code. :-)  Then everybody can judge for
> themselves.

Sure. Here you are:
---cut-------------------------------------------------------------
#!/usr/bin/env ruby

require 'benchmark'

module Foo
  module Sys
    class Shell
      def self.exec cmd
      end
    end
  end
end

Sh2 = Foo::Sys::Shell

module SomewhereIn
  module AnotherNestedModule

    Sh1 = Foo::Sys::Shell
    sh1 = Foo::Sys::Shell

    n = 500000
    Benchmark.bm { |b|
      b.report('idle   :'){n.times{                             }}
      b.report('abs    :'){n.times{   Foo::Sys::Shell::exec('') }}
      b.report('absfq  :'){n.times{ ::Foo::Sys::Shell::exec('') }}
      b.report('var    :'){n.times{               sh1::exec('') }}
      b.report('cnst   :'){n.times{               Sh1::exec('') }}
      b.report('cnstfq :'){n.times{             ::Sh2::exec('') }}
    }
  end
end
---cut-------------------------------------------------------------

>> However it occurred to me that there is a difference between
>> namespace aliases in PHP and the closest thing in Ruby (as
>> described above): Autoloading.
>>
>> In Ruby the assignment to a constant or variable will not work
>> if the module/class has not been loaded (obviously) whereas in
>> PHP I can assign an alias and never use the class.
>>
>> ---PHP-----------------------------------------------------------
>> use \foovendor\system\Shell as Sh;
>> if (false) Sh::exec('ls');
>> if (false) Sh::exec('ls');
>> -----------------------------------------------------------------
>>
>> vs.
>>
>> ---Ruby----------------------------------------------------------
>> Sh = FooVendor::System::Shell
>> if false; Sh::exec(''); end
>> if false; Sh::exec(''); end
>> -----------------------------------------------------------------
>> (unnecessary autoload)

>> Whatever. Maybe namespace aliasing could be a nice addition to
>> Ruby but for the time being the unnecessary autoload wins.
> 
> Well, if you alias a namespace that you never use then you have
> created dead code already.

Nope. The condition (false) in the examples above is rather
simplistic for the sake of clarity. But you can easily have a
conditional branch that is not dead code.

if (today == 'monday' || entropy > 60)
	Sh::exec( 'run some cleanup tasks' )
end


  Philipp