[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

require of a .file

Nicholas Van Weerdenburg

1/17/2005 5:46:00 AM

Hi all,

I'm attempting to do a user config file as a ruby script to be
required- e.g. ~/.myrc

load ENV['HOME'] + "/.myrc"

works, but "require" fails in its place saying it can't find .myrc. Is
this a bug in require?

Thanks,
Nick
--
Nicholas Van Weerdenburg


9 Answers

Yukihiro Matsumoto

1/17/2005 6:37:00 AM

0

Hi,

In message "Re: require of a .file"
on Mon, 17 Jan 2005 14:46:22 +0900, Nicholas Van Weerdenburg <vanweerd@gmail.com> writes:

|I'm attempting to do a user config file as a ruby script to be
|required- e.g. ~/.myrc
|
|load ENV['HOME'] + "/.myrc"
|
|works, but "require" fails in its place saying it can't find .myrc. Is
|this a bug in require?

Show us failing code.

matz.


Greg Millam

1/17/2005 7:01:00 AM

0

Nicholas Van Weerdenburg wrote:
> I'm attempting to do a user config file as a ruby script to be
> required- e.g. ~/.myrc
>
> load ENV['HOME'] + "/.myrc"
> works, but "require" fails in its place saying it can't find .myrc. Is
> this a bug in require?

'require' tacks on ".rb" and ".so" to the filename to find it.

So unless it's named ".myrc.rb," require won't find it.

- Greg


Nicholas Van Weerdenburg

1/17/2005 7:09:00 AM

0

Ah. Thanks. That would be it.

My goal was to use the .xxxrc convention, but actually execute it
rather then read and parse it. I expect "load" should suffice.

Does anyone know of any ruby code that does something similar?

Thanks,
Nick


On Mon, 17 Jan 2005 16:01:20 +0900, Greg Millam
<ruby-talk@lethalcode.net> wrote:
> Nicholas Van Weerdenburg wrote:
> > I'm attempting to do a user config file as a ruby script to be
> > required- e.g. ~/.myrc
> >
> > load ENV['HOME'] + "/.myrc"
> > works, but "require" fails in its place saying it can't find .myrc. Is
> > this a bug in require?
>
> 'require' tacks on ".rb" and ".so" to the filename to find it.
>
> So unless it's named ".myrc.rb," require won't find it.
>
> - Greg
>
>


--
Nicholas Van Weerdenburg


James Gray

1/17/2005 2:27:00 PM

0

On Jan 17, 2005, at 1:09 AM, Nicholas Van Weerdenburg wrote:

> Ah. Thanks. That would be it.
>
> My goal was to use the .xxxrc convention, but actually execute it
> rather then read and parse it. I expect "load" should suffice.
>
> Does anyone know of any ruby code that does something similar?

Sure. Open the file, slurp it and call eval().

James Edward Gray II



Patrick May

1/17/2005 9:25:00 PM

0

Hello,

On Monday, January 17, 2005, at 12:46 AM, Nicholas Van Weerdenburg
wrote:

> Hi all,
>
> I'm attempting to do a user config file as a ruby script to be
> required- e.g. ~/.myrc
>
> load ENV['HOME'] + "/.myrc"
>
> works, but "require" fails in its place saying it can't find .myrc. Is
> this a bug in require?

I made an old rcr about this:

http://www.rcrchive.net/rc...

We should resubmit it?

~ Patrick



Nicholas Van Weerdenburg

1/18/2005 4:03:00 AM

0

Thanks. That worked for me.


On Mon, 17 Jan 2005 23:27:08 +0900, James Edward Gray II
<james@grayproductions.net> wrote:
> On Jan 17, 2005, at 1:09 AM, Nicholas Van Weerdenburg wrote:
>
> > Ah. Thanks. That would be it.
> >
> > My goal was to use the .xxxrc convention, but actually execute it
> > rather then read and parse it. I expect "load" should suffice.
> >
> > Does anyone know of any ruby code that does something similar?
>
> Sure. Open the file, slurp it and call eval().
>
> James Edward Gray II
>
>


--
Nicholas Van Weerdenburg


Nicholas Van Weerdenburg

1/19/2005 12:24:00 AM

0

load works, but require would be nice.

I'm now trying to understand exactly how load works.

load(filename, wrap =false)

Didn't do what I wanted, so I went with the following
lines=File.read(filename)
eval(lines)

Which did work.

Docs for Kernel#load mention:
"In no circumstance will any local variables in the loaded file be
propagated to the loading environment."

Any ideas for how to use load and still set instance variables?

Thanks,
Nick

On Tue, 18 Jan 2005 06:25:05 +0900, Patrick May <patrick@hexane.org> wrote:
> Hello,
>
> On Monday, January 17, 2005, at 12:46 AM, Nicholas Van Weerdenburg
> wrote:
>
> > Hi all,
> >
> > I'm attempting to do a user config file as a ruby script to be
> > required- e.g. ~/.myrc
> >
> > load ENV['HOME'] + "/.myrc"
> >
> > works, but "require" fails in its place saying it can't find .myrc. Is
> > this a bug in require?
>
> I made an old rcr about this:
>
> http://www.rcrchive.net/rc...
>
> We should resubmit it?
>
> ~ Patrick
>
>


Joel VanderWerf

1/19/2005 7:03:00 AM

0

Nicholas Van Weerdenburg wrote:
> load works, but require would be nice.
>
> I'm now trying to understand exactly how load works.
>
> load(filename, wrap =false)
>
> Didn't do what I wanted, so I went with the following
> lines=File.read(filename)
> eval(lines)
>
> Which did work.
>
> Docs for Kernel#load mention:
> "In no circumstance will any local variables in the loaded file be
> propagated to the loading environment."
>
> Any ideas for how to use load and still set instance variables?

Did you mean _local_ vars? (If you assign instance vars in the top level
of a loaded file, they will still be there in the "main" object at the
top level when you get back to the file that is calling load.)

AFAIK, local vars are lost, unless you play games with proc bindings and
eval:

$ cat a.rb
x=1
$pr = proc {}

$ cat b.rb
load 'a.rb'
p(eval("x", $pr))

$ ruby b.rb
1

But this just changes the problem: how to get the proc out of the loaded
file without using something as gauche as a global var? (And never mind
that using "eval" is _extremely_ gauche, and you will not be invited to
ruby parties.)

Here's an alternative that Nobu Nokada suggested back in ruby-talk:62727:

$ cat c.rb
@ivar = 1
CONST = 2
def self.meth; 3; end

$ cat d.rb
class Module
def load_in_module(file)
module_eval(IO.read(file), File.expand_path(file))
end
end

module M
load_in_module('c.rb')
p @ivar
end
p M::CONST
p M.meth
p @ivar

$ ruby d.rb
1
2
3
nil


This lets you can capture top-level constants, methods, and attrs from
the loaded file. That's the basic idea in my script lib
(http://redshift.sourceforge.n...), but it adds some bells and
whistles: it defines require so that local dependencies within the
loaded file work; there is an autoscript (like autoload); meaningful
exceptions; compact syntax; a rudimentary facility for passing params
_in_ to a loaded script.


Nicholas Van Weerdenburg

1/25/2005 6:53:00 AM

0

On Wed, 19 Jan 2005 16:02:46 +0900, Joel VanderWerf
<vjoel@path.berkeley.edu> wrote:
> Nicholas Van Weerdenburg wrote:
> > load works, but require would be nice.
> >
> > I'm now trying to understand exactly how load works.
> >
> > load(filename, wrap =false)
> >
> > Didn't do what I wanted, so I went with the following
> > lines=File.read(filename)
> > eval(lines)
> >
> > Which did work.
> >
> > Docs for Kernel#load mention:
> > "In no circumstance will any local variables in the loaded file be
> > propagated to the loading environment."
> >
> > Any ideas for how to use load and still set instance variables?
>
> Did you mean _local_ vars? (If you assign instance vars in the top level
> of a loaded file, they will still be there in the "main" object at the
> top level when you get back to the file that is calling load.)
>

load with instance variables didn't work me- I had to go with eval.
I'll double check though.

I'm using instances variables, but may switch to local since it reads
nicer in a config file and I'm using eval (1.1 young kids, so I can't
go to parties anyway :)).

> AFAIK, local vars are lost, unless you play games with proc bindings and
> eval:
>
> $ cat a.rb
> x=1
> $pr = proc {}
>
> $ cat b.rb
> load 'a.rb'
> p(eval("x", $pr))
>
> $ ruby b.rb
> 1
>
> But this just changes the problem: how to get the proc out of the loaded
> file without using something as gauche as a global var? (And never mind
> that using "eval" is _extremely_ gauche, and you will not be invited to
> ruby parties.)
>

Nice example. I just read an article yesterday on bindings by Jim
Weirch, coincidently.

> Here's an alternative that Nobu Nokada suggested back in ruby-talk:62727:
>
> $ cat c.rb
> @ivar = 1
> CONST = 2
> def self.meth; 3; end
>
> $ cat d.rb
> class Module
> def load_in_module(file)
> module_eval(IO.read(file), File.expand_path(file))
> end
> end
>
> module M
> load_in_module('c.rb')
> p @ivar
> end
> p M::CONST
> p M.meth
> p @ivar
>
> $ ruby d.rb
> 1
> 2
> 3
> nil
>
> This lets you can capture top-level constants, methods, and attrs from
> the loaded file. That's the basic idea in my script lib
> (http://redshift.sourceforge.n...), but it adds some bells and
> whistles: it defines require so that local dependencies within the
> loaded file work; there is an autoscript (like autoload); meaningful
> exceptions; compact syntax; a rudimentary facility for passing params
> _in_ to a loaded script.
>
>
I will look at it when I get a chance.

Thanks,
Nick
--
Nicholas Van Weerdenburg