require of a .file

  • Thread starter Nicholas Van Weerdenburg
  • Start date
N

Nicholas Van Weerdenburg

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
 
Y

Yukihiro Matsumoto

Hi,

In message "Re: require of a .file"

|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.
 
G

Greg Millam

Nicholas said:
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
 
N

Nicholas Van Weerdenburg

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


Nicholas said:
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
 
J

James Edward Gray II

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
 
N

Nicholas Van Weerdenburg

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

Hello,

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/rcr/show/149

We should resubmit it?

~ Patrick
 
J

Joel VanderWerf

Nicholas said:
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.net/script/), 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.
 
N

Nicholas Van Weerdenburg

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.net/script/), 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top