require of a .file

Discussion in 'Ruby' started by Nicholas Van Weerdenburg, Jan 17, 2005.

  1. 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
     
    Nicholas Van Weerdenburg, Jan 17, 2005
    #1
    1. Advertising

  2. Hi,

    In message "Re: require of a .file"
    on Mon, 17 Jan 2005 14:46:22 +0900, Nicholas Van Weerdenburg <> 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.
     
    Yukihiro Matsumoto, Jan 17, 2005
    #2
    1. Advertising

  3. Nicholas Van Weerdenburg

    Greg Millam Guest

    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
     
    Greg Millam, Jan 17, 2005
    #3
  4. 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
    <> 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
     
    Nicholas Van Weerdenburg, Jan 17, 2005
    #4
  5. 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
     
    James Edward Gray II, Jan 17, 2005
    #5
  6. Nicholas Van Weerdenburg

    Patrick May Guest

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

    We should resubmit it?

    ~ Patrick
     
    Patrick May, Jan 17, 2005
    #6
  7. Thanks. That worked for me.


    On Mon, 17 Jan 2005 23:27:08 +0900, James Edward Gray II
    <> 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, Jan 18, 2005
    #7
  8. 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 <> 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/rcr/show/149
    >
    > We should resubmit it?
    >
    > ~ Patrick
    >
    >
     
    Nicholas Van Weerdenburg, Jan 19, 2005
    #8
  9. 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.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.
     
    Joel VanderWerf, Jan 19, 2005
    #9
  10. On Wed, 19 Jan 2005 16:02:46 +0900, Joel VanderWerf
    <> 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.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
    --
    Nicholas Van Weerdenburg
     
    Nicholas Van Weerdenburg, Jan 25, 2005
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ruby Ruby
    Replies:
    0
    Views:
    191
    Ruby Ruby
    Apr 22, 2004
  2. Zev Blut
    Replies:
    10
    Views:
    178
    Zev Blut
    Feb 23, 2005
  3. Trans
    Replies:
    4
    Views:
    100
    Trans
    Aug 12, 2006
  4. DocPneumo
    Replies:
    4
    Views:
    232
    DocPneumo
    Jan 17, 2007
  5. Vijoy Varghese
    Replies:
    4
    Views:
    130
    Vijoy Varghese
    Jul 11, 2005
Loading...

Share This Page