Loading data (variables)

C

Chris Game

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!
 
B

Brian Schröder

Hi, I'm having some trouble loading dummy data into an irb session;
I can set require=3D"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?
=20
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!

If you are not concerned about security, you could use

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

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

regards,

Brian

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

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

ts

"B" == =?ISO-8859-1?Q?Brian Schr=F6der?= <ISO-8859-1> writes:

B> bschroed@black:~/svn/projekte/ruby-things$ echo "test = 12" > data.rb
B> bschroed@black:~/svn/projekte/ruby-things$ irb
B> irb(main):001:0> eval(File.read('data.rb'))
B> => 12
B> irb(main):002:0> test
B> => 12
B> 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
 
C

Chris Game

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

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

Chris Game

Brian said:
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
 

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

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,244
Latest member
cryptotaxsoftware12

Latest Threads

Top