2010/8/31 Philipp Kempgen <lists / kempgen.net>:
> Iain Barnett wrote:
>> On 31 Aug 2010, at 02:07, Joel VanderWerf wrote:
>>
>>> Use local vars or (scoped) contants?
>>
>> Excellent, thanks. I didn't realise they could just be stuck into vars.
>
>> Going back in time would be cool if it was in a time machine, but throwing out the good things learned over the years about things in computing seems the wrong way to do it. It's why I don't use PHP ;)
>
> 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'

> And I like the convention (not imposed by PHP) to use lowercase
> names for namespaces. In the example above it is immediately clear
> that "\foovendor\system" is a namespace and that "Shell" is a class.
>
> BTW: Given the Ruby example in following
>
> module A
>  >   >  
> end
>
> it's still not clear to me if I should refer to the X class as
> A::B::C or as ::A::B::C to make it obvious that this refers to
> A::B::C in the top-level scope (::) and to avoid a lookup in the
> local scope. (What are the best practices?)

Neither - you would reference via A::B::X or ::A::B::X. :-)

Dunno whether there is a *best* practice.  If you want to be on the
safe side when accessing classes and modules outside your current
module hierarchy then you must use the "::" prefix.  If you have many
classes and modules in your namespace it's probably best to anchor
lookups in the global namespace to avoid issues.  Generally though
people seem to be using unprefixed names - especially for frequently
used classes like String and Hash.

What seems to be an issue for you (or for people coming from PHP) does
not seem to be an issue for most Ruby developers (at least if my
feeble memory of discussions here does not fail me).  It's
understandable but it might be easier to stuff PHP experience in the
closet and try to approach Ruby with less historic baggage. :-)

> My benchmarks show no significant performance improvement though
> (whereas there is a noticable improvement in PHP).
>
> Actually if you want the maximum performance out of PHP you end up
> using fully qualified identifiers everywhere, e.g. \strlen instead
> of strlen and \true instead of true (oh boy).

My goodness!  Another reason to abhor PHP.  Please do not import any
bad practices from PHP to Ruby - at least not when writing libraries
that you intend to release into the public. :-)

Kind regards

robert

-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/