Loading data (variables)

Hi, I'm having some trouble loading dummy data into an irb session;
I can set require="somefile" to include Classes and methods from a
file, but the set of data assignments I have in another file I
cannot load - I get the 'NameError: undefined local variable...'
message when I try to load it and then examine the data afterwards.
The only way I can get the data into the irb session is by copy and
paste - I'm sure there must be a better way?

I see there's some comment in the 'Pickaxe' about scope and local
variables from a loaded file on p.124 but I'm not at all clear as to
what to do to get them visible at the irb prompt!

···

--
Chris Game

"Just because I don't care doesn't mean I don't understand."
-- Homer Simpson

If you are not concerned about security, you could use

bschroed@black:~/svn/projekte/ruby-things$ echo "test = 12" > data.rb
bschroed@black:~/svn/projekte/ruby-things$ irb
irb(main):001:0> eval(File.read('data.rb'))
=> 12
irb(main):002:0> test
=> 12
irb(main):003:0>

but if someone could smuggle "system 'rm -rf /'" into data.rb you have
got a real problem.

regards,

Brian

···

On 30/07/05, Chris Game <chrisgame@example.net> wrote:

Hi, I'm having some trouble loading dummy data into an irb session;
I can set require="somefile" to include Classes and methods from a
file, but the set of data assignments I have in another file I
cannot load - I get the 'NameError: undefined local variable...'
message when I try to load it and then examine the data afterwards.
The only way I can get the data into the irb session is by copy and
paste - I'm sure there must be a better way?

I see there's some comment in the 'Pickaxe' about scope and local
variables from a loaded file on p.124 but I'm not at all clear as to
what to do to get them visible at the irb prompt!

--
http://ruby.brian-schroeder.de/

Stringed instrument chords: http://chordlist.brian-schroeder.de/

bschroed@black:~/svn/projekte/ruby-things$ echo "test = 12" > data.rb
bschroed@black:~/svn/projekte/ruby-things$ irb
irb(main):001:0> eval(File.read('data.rb'))
=> 12
irb(main):002:0> test
=> 12
irb(main):003:0>

moulon% echo "test = 12" > data.rb
moulon%

moulon% irb
irb(main):001:0> eval('$SAFE=4;' + File.read('data.rb'))
=> 12
irb(main):002:0> test
=> 12
irb(main):003:0> moulon%

Guy Decoux

Brian Schröder wrote:

irb(main):001:0> eval(File.read('data.rb'))

Thanks - that appears to do the trick. although I'm still not clear
why 'load"filename" ' doesn't make the data available and eval(...)
does.

···

--
Chris Game

"I do not write for such dull elves,
As have not a great deal of ingenuity themselves."
   -- Jane Austen

ts <decoux@moulon.inra.fr> writes:

> bschroed@black:~/svn/projekte/ruby-things$ echo "test = 12" > data.rb
> bschroed@black:~/svn/projekte/ruby-things$ irb
> irb(main):001:0> eval(File.read('data.rb'))
> => 12
> irb(main):002:0> test
> => 12
> irb(main):003:0>

moulon% echo "test = 12" > data.rb
moulon%

echo "test = 12; BEGIN { p $SAFE }" > data.rb

moulon% irb
irb(main):001:0> eval('$SAFE=4;' + File.read('data.rb'))

0
=> 12

pwned :smiley:

···

Guy Decoux

--
Christian Neukirchen <chneukirchen@gmail.com> http://chneukirchen.org

Chris Game <chrisgame@example.net> writes:

Brian Schröder wrote:

irb(main):001:0> eval(File.read('data.rb'))

Thanks - that appears to do the trick. although I'm still not clear
why 'load"filename" ' doesn't make the data available and eval(...)
does.

Because it is exactly made for that:

------------------------------------------------------------ Kernel#load
     load(filename, wrap=false) => true

···

------------------------------------------------------------------------
     Loads and executes the Ruby program in the file _filename_. If the
     filename does not resolve to an absolute path, the file is searched
     for in the library directories listed in +$:+. If the optional
     _wrap_ parameter is +true+, the loaded script will be executed
     under an anonymous module, protecting the calling program's global

namespace. In no circumstance will any local variables in the <<<
loaded file be propagated to the loading environment. <<<

Chris Game

--
Christian Neukirchen <chneukirchen@gmail.com> http://chneukirchen.org

Christian Neukirchen wrote:

echo "test = 12; BEGIN { p $SAFE }" > data.rb

So what's this BEGIN {..} stuff do then?

···

--
Chris Game

"A witty saying proves nothing." -- Voltaire

pwned :smiley:

eval is evil

Guy Decoux

Chris Game <chrisgame@example.net> writes:

Christian Neukirchen wrote:

echo "test = 12; BEGIN { p $SAFE }" > data.rb

So what's this BEGIN {..} stuff do then?

It gets executed before the $SAFE = 4 line that is getting prepended.
If there is malicious code in BEGIN {}, you lose.

···

Chris Game

--
Christian Neukirchen <chneukirchen@gmail.com> http://chneukirchen.org

Christian Neukirchen wrote:

Chris Game <chrisgame@example.net> writes:

Christian Neukirchen wrote:

echo "test = 12; BEGIN { p $SAFE }" > data.rb

So what's this BEGIN {..} stuff do then?

It gets executed before the $SAFE = 4 line that is getting
prepended. If there is malicious code in BEGIN {}, you lose.

Chris Game

Thanks - but does it always get executed before other lines which go
through the eval(..) statement in irb? It looks like irb executes
all lines in order.

···

--
Chris Game

"Chance favors only the prepared mind." -- Louis Pasteur