Equivalent of "source" in ruby?

T

Todd A. Jacobs

Contrary to my expectations, if I have some variable assignments in an
external file, I can't simply call:

load rcfile

to have it sourced. Instead, I found myself having to do this:

IO.foreach(rcfile) do |line|
eval line
end

which seems lame. Is there a better way?
 
A

Alex Gutteridge

Contrary to my expectations, if I have some variable assignments in an
external file, I can't simply call:

load rcfile

to have it sourced. Instead, I found myself having to do this:

IO.foreach(rcfile) do |line|
eval line
end

which seems lame. Is there a better way?

Would globals work for you?

[alexg@powerbook]/Users/alexg/Desktop(28): cat load_me.rb
$global=10
local=20
[alexg@powerbook]/Users/alexg/Desktop(29): cat main.rb
load 'load_me.rb'

puts $global
puts local
[alexg@powerbook]/Users/alexg/Desktop(30): ruby main.rb
10
main.rb:4: undefined local variable or method `local' for main:Object
(NameError)

Alex Gutteridge

Bioinformatics Center
Kyoto University
 
M

Michael T. Richter

--=-WcM5+U4SL0cR+iGFMkvS
Content-Type: multipart/alternative; boundary="=-AWc4XdvKKeRBoE7GubRp"


--=-AWc4XdvKKeRBoE7GubRp
Content-Type: text/plain
Content-Transfer-Encoding: quoted-printable

Contrary to my expectations, if I have some variable assignments in an
external file, I can't simply call:
=20
load rcfile
=20
to have it sourced. Instead, I found myself having to do this:
=20
IO.foreach(rcfile) do |line|
eval line
end
=20
which seems lame. Is there a better way?



#test1.rb:

@a =3D 5
@b =3D 4
=20
require 'test2'
=20
p @a
p @b
p @c

#----8<-----

#test2.rb:

@b =3D 3
@c =3D 2


$ ruby test1.rb
5
3
2
$

Or am I missing something?
--=20
Michael T. Richter <[email protected]> (GoogleTalk:
(e-mail address removed))
So much of what we call management consists in making it difficult for
people to work. (Peter Drucker)

--=-AWc4XdvKKeRBoE7GubRp
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV=3D"Content-Type" CONTENT=3D"text/html; CHARSET=3DUTF-8">
<META NAME=3D"GENERATOR" CONTENT=3D"GtkHTML/3.12.1">
</HEAD>
<BODY>
On Fri, 2007-07-09 at 14:17 +0900, Todd A. Jacobs wrote:
<BLOCKQUOTE TYPE=3DCITE>
<PRE>
<FONT COLOR=3D"#000000">Contrary to my expectations, if I have some variabl=
e assignments in an</FONT>
<FONT COLOR=3D"#000000">external file, I can't simply call:</FONT>

<FONT COLOR=3D"#000000"> load rcfile</FONT>

<FONT COLOR=3D"#000000">to have it sourced. Instead, I found myself having =
to do this:</FONT>

<FONT COLOR=3D"#000000"> IO.foreach(rcfile) do |line|</FONT>
<FONT COLOR=3D"#000000"> eval line</FONT>
<FONT COLOR=3D"#000000"> end</FONT>

<FONT COLOR=3D"#000000">which seems lame. Is there a better way?</FONT>
</PRE>
</BLOCKQUOTE>
<PRE>

</PRE>
#test1.rb:<BR>
<BLOCKQUOTE>
<TT>@a =3D 5</TT><BR>
<TT>@b =3D 4</TT><BR>
<BR>
<TT>require 'test2'</TT><BR>
<BR>
<TT>p @a</TT><BR>
<TT>p @b</TT><BR>
<TT>p @c</TT><BR>
</BLOCKQUOTE>
#----8&lt;-----<BR>
<BR>
#test2.rb:<BR>
<BLOCKQUOTE>
<TT>@b =3D 3</TT><BR>
<TT>@c =3D 2</TT><BR>
</BLOCKQUOTE>
<BR>
<TT>$ </TT><TT><B>ruby test1.rb</B></TT><BR>
<I><TT>5</TT></I><BR>
<I><TT>3</TT></I><BR>
<I><TT>2</TT></I><BR>
<TT>$</TT><BR>
<BR>
Or am I missing something?<BR>
<TABLE CELLSPACING=3D"0" CELLPADDING=3D"0" WIDTH=3D"100%">
<TR>
<TD>
-- <BR>
<B>Michael T. Richter</B> &lt;<A HREF=3D"mailto:[email protected]">ttmri=
(e-mail address removed)</A>&gt; (<B>GoogleTalk:</B> (e-mail address removed))<BR>
<I>So much of what we call management consists in making it difficult for p=
eople to work. (Peter Drucker)</I>
</TD>
</TR>
</TABLE>
</BODY>
</HTML>

--=-AWc4XdvKKeRBoE7GubRp--

--=-WcM5+U4SL0cR+iGFMkvS
Content-Type: application/pgp-signature; name=signature.asc
Content-Description: This is a digitally signed message part

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.3 (GNU/Linux)

iD8DBQBG4OPmLqyWkKVQ54QRAonVAJ9VAX9SOQ/2Q6wq5btN/cCSijdgIgCfXVBn
D7TCmkerXek1CS9KkDRrCIY=
=g3BK
-----END PGP SIGNATURE-----

--=-WcM5+U4SL0cR+iGFMkvS--
 
E

Erik Veenstra

Contrary to my expectations, if I have some variable
assignments in an external file, I can't simply call:

load rcfile

Load starts a new scope without reusing the local scope. Eval
starts a new scope with reusing the local scope. So, use eval
instead. But you should "define" the vars before loading them.

gegroet,
Erik V. - http://www.erikveen.dds.nl/

----------------------------------------------------------------

$ cat test.rb
a=nil
b=nil

eval(File.read("vars.rb"))

# Or better: Thread.new{$SAFE=4 ; eval(File.read("vars.rb"),
Module.new.instance_eval{binding})}.join

p a
p b

----------------------------------------------------------------

$ cat vars.rb
a = 7
b = 8

----------------------------------------------------------------

$ ruby test.rb
7
8

----------------------------------------------------------------
 
R

Robert Klemme

2007/9/7 said:
On Fri, 2007-07-09 at 14:17 +0900, Todd A. Jacobs wrote:
Contrary to my expectations, if I have some variable assignments in an
external file, I can't simply call:

load rcfile

to have it sourced. Instead, I found myself having to do this:

IO.foreach(rcfile) do |line|
eval line
end

which seems lame. Is there a better way?


#test1.rb:

@a = 5
@b = 4

require 'test2'

p @a
p @b
p @c
#----8<-----

#test2.rb:

@b = 3
@c = 2

$ ruby test1.rb
5
3
2
$

Or am I missing something?

Yes, you are missing the local variable bit. :)

robert
 
R

Robert Klemme

2007/9/7 said:
Contrary to my expectations, if I have some variable assignments in an
external file, I can't simply call:

load rcfile

to have it sourced. Instead, I found myself having to do this:

IO.foreach(rcfile) do |line|
eval line
end

which seems lame. Is there a better way?

It is also unsafe - not only because of the eval but also because this
will give errors for expressions that span multiple lines. The easy
fix would be

eval(File.read(rc_file))

But I'd rather resort to one of the other suggestions (namely using
local variables).

Kind regards

robert
 
E

Erik Veenstra

a = nil
b = nil

Thread.new do
data = File.read("vars.rb")
$SAFE = 4
b = Module.new.instance_eval{binding}

eval(data, b)
end.join

p a
p b
 
M

Michael T. Richter

--=-C148hUtIf3bSxrvqjemc
Content-Type: multipart/related; type="multipart/alternative";
boundary="=-KFCgkptCnS8voryPZLQu"


--=-KFCgkptCnS8voryPZLQu
Content-Type: multipart/alternative; boundary="=-g9d3I1ButlRoyjuis29O"


--=-g9d3I1ButlRoyjuis29O
Content-Type: text/plain
Content-Transfer-Encoding: quoted-printable

Yes, you are missing the local variable bit. :)


That would do it. :D

--=20
Michael T. Richter <[email protected]> (GoogleTalk:
(e-mail address removed))
When debugging, novices insert corrective code; experts remove defective
code. (Richard Pattis)

--=-g9d3I1ButlRoyjuis29O
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV=3D"Content-Type" CONTENT=3D"text/html; CHARSET=3DUTF-8">
<META NAME=3D"GENERATOR" CONTENT=3D"GtkHTML/3.12.1">
</HEAD>
<BODY>
On Fri, 2007-07-09 at 19:53 +0900, Robert Klemme wrote:
<BLOCKQUOTE TYPE=3DCITE>
<PRE>
<FONT COLOR=3D"#000000">Yes, you are missing the local variable bit. :)</F=
ONT>
</PRE>
</BLOCKQUOTE>
<BR>
That would do it.&nbsp; <IMG SRC=3D"cid:1189167647.23619.17.camel@localhost=
localdomain" ALIGN=3D"middle" ALT=3D":D" BORDER=3D"0"><BR>
<BR>
<TABLE CELLSPACING=3D"0" CELLPADDING=3D"0" WIDTH=3D"100%">
<TR>
<TD>
-- <BR>
<B>Michael T. Richter</B> &lt;<A HREF=3D"mailto:[email protected]">ttmri=
(e-mail address removed)</A>&gt; (<B>GoogleTalk:</B> (e-mail address removed))<BR>
<I>When debugging, novices insert corrective code; experts remove defective=
code. (Richard Pattis)</I>
</TD>
</TR>
</TABLE>
</BODY>
</HTML>

--=-g9d3I1ButlRoyjuis29O--

--=-KFCgkptCnS8voryPZLQu
Content-ID: <[email protected]>
Content-Disposition: attachment; filename=smiley-1.png
Content-Type: image/png; name=smiley-1.png
Content-Transfer-Encoding: base64

iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAC5UlEQVR42n2TW2hUZxSFv/8/Z6Jz
caKJwTGhyWi9glSUglYsRZFWECvYRir6ZL3jJRZLsYJQtAj6oC2IISBqQQg+SKOEKiRStUZ9EbyM
NcPEGSXRmMyJTjLnZOb8Z87fh6RCrbhgP+y1914va234P+qAHUAL4I7W78CW0dl/IN7qT86Kh7bu
2ziP5V/MxgzHoVTAHXpMR8df7P91gMfP3EZg27sE2n/aNnfpzl3fIIwgnp3BzScRwsAYOxmjrBJl
pzn2SxtHmrPtwDIAY/S46eCOj7/euXsdpeEelJPBVzmEDADguxaenQY0nyyIYzh9U28+LMSAVgHU
zZ05IXP1wh60N4Q33I0wgpR8iSF9dGkYt+hgYON7DmawmuLre3y++QnJ7lJcAitajlUgZADlPKXt
VpZNP14nvvg0ucCXHD3VybRll6j/LkE2tBuV78IMxthcPx5gkQAas/d/2KLspwDIQJRQzVo85aCd
RwgzTCAyC60scqkT+CqHNEPcudvFV3t7z5s1NdXfAvjeINKMgPYYfnEBIcvQvkKX8hT6r+F7NlAC
wCv0MXmiAbDatCyLP/58QiaVougFEDKAxkRKA7SP1mpEyC/iey4ClzLDZUJ0xECzUCi2tt/oXNWw
/zxKKSorKzFNk6qqqjf+aq3RWmNZFkopbNum9cwawLpjAlc+qn25KhbuQpZ/ipSS8vJytNZIKVFK
IYRACIGUEsdxKOYSdD7IADQbQH/mOQ0zYt18UJElk51IMpnEsiwAwuEwvb29pNNpEokEk6KvqBg8
zKGmbgby7DGAnJVTtTVRb96YQI45M8ZT/eESamvjRCIRpJQYxkjeqsblCfcf4PuDD7ie8BuB3/5N
4sXbDwc/i8r8FG/oBsJqJlhWwHPzDPSl6O+5z4vUZSapJn4+nuBsu7oG1L/rmU5NickNKxeG2Lt9
+hvyeZ/LlY7XnGvpIdnDOWA970Ed0ADcAhRgAX8DJ4D5by//A0f8RUIabvq3AAAAAElFTkSuQmCC



--=-KFCgkptCnS8voryPZLQu--

--=-C148hUtIf3bSxrvqjemc
Content-Type: application/pgp-signature; name=signature.asc
Content-Description: This is a digitally signed message part

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.3 (GNU/Linux)

iD8DBQBG4UIhLqyWkKVQ54QRAuy6AJ9SeMyjULSWeQKlm3mVPcdm1+zdiACeMeKN
Nv37pRpi0w/x81xZHitUI0E=
=ovUQ
-----END PGP SIGNATURE-----

--=-C148hUtIf3bSxrvqjemc--
 
E

Eric Hodel

a = nil
b = nil

Thread.new do
data = File.read("vars.rb")
$SAFE = 4
b = Module.new.instance_eval{binding}

eval(data, b)
end.join

p a
p b

echo "Thread.critical = true; sleep" >> vars.rb
 
R

Robert Klemme

2007/9/7 said:
It is also unsafe - not only because of the eval but also because this
will give errors for expressions that span multiple lines. The easy
fix would be

eval(File.read(rc_file))

But I'd rather resort to one of the other suggestions (namely using
local variables).

That was intended to read "global" of course. Sorry for the noise.

robert
 

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

Staff online

Members online

Forum statistics

Threads
474,264
Messages
2,571,323
Members
48,007
Latest member
TerranceCo

Latest Threads

Top