[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Autoloading objects — Ruby equivalent for PHP5 "__autoload

Naum T.

7/24/2006 9:59:00 AM

Does there exist a Ruby equivalent to PHP5 "magic" __autoload method
that spares the developer from detailing a list of "require" statements?
http://us2.php.ne...

Or is this something that a homebrewed solution is necessary? Like
catching the exception and testing for file existence, etc.â?¦ or is there
a gem/extension featuring this functionality?

--
Posted via http://www.ruby-....

11 Answers

Marcin Mielzynski

7/24/2006 12:17:00 PM

0

Naum T. wrote:
> Does there exist a Ruby equivalent to PHP5 "magic" __autoload method
> that spares the developer from detailing a list of "require" statements?
> http://us2.php.ne...
>
> Or is this something that a homebrewed solution is necessary? Like
> catching the exception and testing for file existence, etc.â?¦ or is there
> a gem/extension featuring this functionality?
>

Sure there is ;D

Kernel#autoload:
http://ruby-doc.org/core/classes/Kernel.ht...

and:

Module#autoload:
http://ruby-doc.org/core/classes/Module.ht...

lopex

Mat Schaffer

7/24/2006 12:38:00 PM

0


On Jul 24, 2006, at 5:58 AM, Naum T. wrote:

> Does there exist a Ruby equivalent to PHP5 "magic" __autoload method
> that spares the developer from detailing a list of "require"
> statements?
> http://us2.php.ne...
>
> Or is this something that a homebrewed solution is necessary? Like
> catching the exception and testing for file existence, etc.… or is
> there
> a gem/extension featuring this functionality?


const_missing is a little more like PHP's __autoload function.

The following, for example will attempt to just do: require <object
name> in the event it can't find the class.

def Object.const_missing name
require name.to_s.downcase
const_get(name)
end

I've never used it though, so anyone can feel free to correct me for
any gotchas that I'm not aware of.
-Mat

Alex Young

7/24/2006 12:45:00 PM

0

Marcin MielżyÅ?ski wrote:
> Naum T. wrote:
>
>> Does there exist a Ruby equivalent to PHP5 "magic" __autoload method
>> that spares the developer from detailing a list of "require" statements?
>> http://us2.php.ne...
>>
>> Or is this something that a homebrewed solution is necessary? Like
>> catching the exception and testing for file existence, etc.â?¦ or is
>> there a gem/extension featuring this functionality?
>>
>
> Sure there is ;D
>
> Kernel#autoload:
> http://ruby-doc.org/core/classes/Kernel.ht...
>
> and:
>
> Module#autoload:
> http://ruby-doc.org/core/classes/Module.ht...
That's not quite it. You need to call Module.autoload yourself. PHP's
autoload is called by the interpreter to perform the inclusion. You
could use const_missing to get a similar functionality, though...

def Object.const_missing(name)
load name.to_lower + ".rb"
end

That's got a few rough edges, though, to put it mildly...

--
Alex

Alex Young

7/24/2006 12:54:00 PM

0

Alex Young wrote:
<snip>
> def Object.const_missing(name)
> load name.to_lower + ".rb"
> end
>
> That's got a few rough edges, though, to put it mildly...
>
I mean, besides the fact that it won't work, unlike Mat's. Should have
tested that one...

--
Alex

Naum T.

7/24/2006 3:45:00 PM

0

That's seems to be the ticket.

Knew about ruby autoload, but you have to specify up front each
module/file whereis PHP5, it's (but named "__autoload") a "catch all".

One solution is to glob the directory (where I have my classes stored)
and issue autoload for each result:

# assumes my custom classes begin with upper case
Dir.glob('[A-Z]*.rb').each do |f|
rsym = f.gsub("[.]rb$", '')
autoload(:#{rsym}, f)
end

>
> const_missing is a little more like PHP's __autoload function.
>
> The following, for example will attempt to just do: require <object
> name> in the event it can't find the class.
>
> def Object.const_missing name
> require name.to_s.downcase
> const_get(name)
> end
>
> I've never used it though, so anyone can feel free to correct me for
> any gotchas that I'm not aware of.
> -Mat


--
Posted via http://www.ruby-....

Matthew Harris

7/24/2006 3:52:00 PM

0

You would probably want to use some metaprogramming to define
const_missing recursively on currently defined constants, and then
it's pretty much the same as PHP's __autoload().

On 7/24/06, Alex Young <alex@blackkettle.org> wrote:
> Alex Young wrote:
> <snip>
> > def Object.const_missing(name)
> > load name.to_lower + ".rb"
> > end
> >
> > That's got a few rough edges, though, to put it mildly...
> >
> I mean, besides the fact that it won't work, unlike Mat's. Should have
> tested that one...
>
> --
> Alex
>
>


--
Matt

Gonzalo Garramuno

7/25/2006 11:23:00 AM

0

Naum T. escribió:
> Does there exist a Ruby equivalent to PHP5 "magic" __autoload method
> that spares the developer from detailing a list of "require" statements?
> http://us2.php.ne...
>
> Or is this something that a homebrewed solution is necessary? Like
> catching the exception and testing for file existence, etc.â?¦ or is there
> a gem/extension featuring this functionality?
>

def Object.const_missing elem
require elem.to_s
end

a = A.new

PS. I would advise *NOT* doing this kind of thing, thou. It can make
your code much harder to debug for other developers.

Mat Schaffer

7/25/2006 1:00:00 PM

0


On Jul 25, 2006, at 7:22 AM, Gonzalo Garramuno wrote:

> Naum T. escribió:
>> Does there exist a Ruby equivalent to PHP5 "magic" __autoload method
>> that spares the developer from detailing a list of "require"
>> statements?
>> http://us2.php.ne...
>>
>> Or is this something that a homebrewed solution is necessary? Like
>> catching the exception and testing for file existence, etc.… or is
>> there
>> a gem/extension featuring this functionality?
>>
>
> def Object.const_missing elem
> require elem.to_s
> end
>
> a = A.new
>
> PS. I would advise *NOT* doing this kind of thing, thou. It can make
> your code much harder to debug for other developers.

you need
> def Object.const_missing elem
> require elem.to_s
const_get(elem)
> end

To make this work, otherwise the 'A' in A.new becomes the return
value of 'require elem.to_s'
-Mat

Matthew Harris

7/25/2006 1:15:00 PM

0

I do hope you know that there's a lot of things in Ruby that makes a
developer's debugging life quite hard, but the point is that it's
convenient to have these sort of solutions.

On 7/25/06, Gonzalo Garramuno <ggarra@advancedsl.com.ar> wrote:
> Naum T. escribió:
> > Does there exist a Ruby equivalent to PHP5 "magic" __autoload method
> > that spares the developer from detailing a list of "require" statements?
> > http://us2.php.ne...
> >
> > Or is this something that a homebrewed solution is necessary? Like
> > catching the exception and testing for file existence, etc.… or is there
> > a gem/extension featuring this functionality?
> >
>
> def Object.const_missing elem
> require elem.to_s
> end
>
> a = A.new
>
> PS. I would advise *NOT* doing this kind of thing, thou. It can make
> your code much harder to debug for other developers.
>
>


--
Matt

Piotr Biedruna

3/20/2007 4:20:00 PM

0

Mat Schaffer wrote:

> const_missing is a little more like PHP's __autoload function.
>
> The following, for example will attempt to just do: require <object
> name> in the event it can't find the class.
>
> def Object.const_missing name
> require name.to_s.downcase
> const_get(name)
> end

Well, th const_missing sollution does work, but only in scripts called
directly in shell by 'ruby' executable. When using it in script called
in browser i get:

[Tue Mar 20 16:56:49 2007] [error] mod_ruby: error in ruby
[Tue Mar 20 16:56:49 2007] [error] mod_ruby:
/home/services/httpd/html/TRON/ruby/index.rb:11: uninitialized constant
#<Module:0xb6a6ea30>::Test (NameError)
[Tue Mar 20 16:56:49 2007] [error] mod_ruby: from
/usr/lib/ruby/1.8/apache/ruby-run.rb:38:in `load'
[Tue Mar 20 16:56:49 2007] [error] mod_ruby: from
/usr/lib/ruby/1.8/apache/ruby-run.rb:38:in `handler'

Got apache 2.2 with mod_ruby configured as follows:

LoadModule ruby_module modules/mod_ruby.so

<IfModule mod_ruby.c>

AddType text/html .rb

RubyRequire apache/ruby-run

<Files *.rb>
Options +ExecCGI
SetHandler ruby-object
RubyHandler Apache::RubyRun.instance
</Files>

</IfModule>

What i`ve been trying to get is an equivalent of PHP __autoload()
routine working under both shell execution and apache handler. Know any?

--
Posted via http://www.ruby-....