Accessing variables when loading a file to irb

Discussion in 'Ruby' started by Ib Lis, Jul 6, 2008.

  1. Ib Lis

    Ib Lis Guest

    Hi
    I'm new to Ruby. Coming from Perl I really do enjoy the syntax and...
    irb.

    When I load a file, classes are loaded but not the variables set in the
    file.

    My file ends like that :

    fields = Set.new
    bibfile.each do | entry |
    entry.properties.keys.each do | field |
    fields << field
    end
    end

    p fields

    After load, irb prints the value of fields but the variable is not in
    scope :

    >> load 'bibtest.2.rb'

    #<Set: {:booktitle, :volume, :pages, :issn, :note, :ps, :number,
    :author, :custom, :year, :title, :journal}>
    => true
    >> fields

    NameError: undefined local variable or method `fields' for main:Object

    Thanks in advance for any hints
    --
    Posted via http://www.ruby-forum.com/.
    Ib Lis, Jul 6, 2008
    #1
    1. Advertising

  2. Ib Lis wrote:
    > Hi
    > I'm new to Ruby. Coming from Perl I really do enjoy the syntax and...
    > irb.
    >
    > When I load a file, classes are loaded but not the variables set in the
    > file.


    Normally(*) in ruby you can't get at the local vars in a file:

    $ cat x.rb
    x = 4
    $ ruby -e 'eval File.read("x.rb"); p x'
    -e:1: undefined local variable or method `x' for main:Object (NameError)

    However, irb is a little different from ruby:

    $ irb
    irb(main):001:0> eval File.read("x.rb"); x
    NameError: undefined local variable or method `x' for main:Object
    from (irb):1
    irb(main):002:0> x
    => 4

    Note that x is undefined if you use it in the same line that defines it,
    but x is defined thereafter. This is because irb parses the whole line
    at a time before executing it. After the eval, x is defined in irb's
    scope. That's special to irb--doing the same eval in a separate file
    doesn't work:

    $ cat y.rb
    eval File.read("x.rb")
    p x
    $ ruby y.rb
    y.rb:2: undefined local variable or method `x' for main:Object (NameError)

    -------------------------
    (*) However:

    $ cat x2.rb
    x = 4
    $scope = proc{}

    $ ruby -e 'eval File.read("x2.rb"); p eval("x", $scope)'
    4

    The global is just one way to break out of the scope of the file. You
    could also use a constant.

    The global might be best if you are just doing this interactively. If
    you need to load files from a program, and keep their instance (not
    local) variables, constants, and methods in separate scopes, you might
    be interested in these:

    http://raa.ruby-lang.org/project/script/
    http://raa.ruby-lang.org/project/dynaload/

    --
    vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407
    Joel VanderWerf, Jul 6, 2008
    #2
    1. Advertising

  3. Thanks a lot. Very clear and useful.

    I'll stick on the irb variant but I like the ruby hack a lot.
    --
    Posted via http://www.ruby-forum.com/.
    عمر ملقب بالثانی, Jul 6, 2008
    #3
  4. Joel VanderWerf wrote:
    > If
    > you need to load files from a program, and keep their instance (not
    > local) variables, constants, and methods in separate scopes, you might
    > be interested in these:
    >
    > http://raa.ruby-lang.org/project/script/


    Ok, I said no local variables, but it's possible to expose them to the
    caller too, using eval(var_name, binding). So anyway, the 0.3 release of
    the above lets you do the following:


    $ cat prog.rb
    require 'script'

    script = Script.load("my-script.rb")

    p eval("x", script.__script_scope)
    p script.__local_variable_get:)x)
    p script.__local_variables
    p script.instance_variables

    $ cat my-script.rb
    x = 4
    require 'my-script-sub'

    $ cat my-script-sub.rb
    x = 5
    y = 6

    $ ruby prog.rb
    4
    4
    ["__file__", "x"]
    ["@__script_scope", "@__loaded_features", "@__dir", "@__main_file"]


    --
    vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407
    Joel VanderWerf, Jul 7, 2008
    #4
  5. Joel VanderWerf wrote:
    > Ok, I said no local variables, but it's possible to expose them to the
    > caller too, using eval(var_name, binding).


    Thanks again. Good to know this.

    --
    Posted via http://www.ruby-forum.com/.
    عمر ملقب بالثانی, Jul 7, 2008
    #5
    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. dwok
    Replies:
    7
    Views:
    697
    Hal Rosser
    Mar 4, 2005
  2. Sam Stephenson
    Replies:
    1
    Views:
    225
    Andrew Walrond
    Jun 18, 2005
  3. Replies:
    1
    Views:
    158
    Florian Groß
    Oct 26, 2005
  4. anne001
    Replies:
    1
    Views:
    269
    anne001
    Jun 27, 2006
  5. Gudni Hilmarsson

    Loading .rb files in IRb

    Gudni Hilmarsson, Dec 24, 2007, in forum: Ruby
    Replies:
    2
    Views:
    131
    Gudni Hilmarsson
    Dec 24, 2007
Loading...

Share This Page