From: Robert Klemme Date: 2005-02-20T23:54:42+09:00 Subject: Re: Getting a method or class within a file ------=_NextPart_000_0016_01C51764.61653BC0 Content-Type: text/plain; format=flowed; charset="iso-8859-1"; reply-type=response Content-Transfer-Encoding: 7bit "Asfand Yar Qazi" schrieb im Newsbeitrag news:37r4gcF5e28v0U1@individual.net... > Hi, > > I want to be able to do the following: > > lots of Ruby files are in a directory, each containing stuff and a method > 'init_file'. I want to be able to 'require' each file, and then call the > 'init_file' method within that file. Do you want to invoke init_file *always* if you require a file or just on every first load? If it's the typical intialization stuff, here are three methods you can use (see attached). > Each file will have its own 'init_file' method, so I can't just do a: > > require 'file' > init_file > > because the init_file method will have been defined before hand. > > Is it possible, like in Perl, for an included file to return a value? I think you can do that with load but not require because require returns whether the file was read or not. Kind regards robert ------=_NextPart_000_0016_01C51764.61653BC0 Content-Type: application/octet-stream; name="req.rb" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="req.rb" =0A= INIT =3D []=0A= =0A= require 'in1'=0A= require 'in2'=0A= require 'in3'=0A= =0A= INIT.each {|fun| fun.call}=0A= ------=_NextPart_000_0016_01C51764.61653BC0 Content-Type: application/octet-stream; name="in3.rb" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="in3.rb" =0A= INIT << lambda { puts "init #{__FILE__}" }=0A= =0A= ------=_NextPart_000_0016_01C51764.61653BC0 Content-Type: application/octet-stream; name="in1.rb" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="in1.rb" =0A= begin=0A= puts "init #{__FILE__}"=0A= end=0A= =0A= ------=_NextPart_000_0016_01C51764.61653BC0 Content-Type: application/octet-stream; name="in2.rb" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="in2.rb" =0A= BEGIN {=0A= puts "init #{__FILE__}"=0A= }=0A= =0A= ------=_NextPart_000_0016_01C51764.61653BC0--