Weird problem with DATA and __END__

M

Michael T. Richter

--=-M3ARuu4mBmyahaZUfWG5
Content-Type: multipart/alternative; boundary="=-0yx/9FOCCP88FVbBYwnC"


--=-0yx/9FOCCP88FVbBYwnC
Content-Type: text/plain
Content-Transfer-Encoding: quoted-printable

File: test1.rb:

require 'test2'
puts "test1.rb =3D " + DATA.readlines.to_s
__END__
This is the data in test1.rb.


File: test2.rb:

puts "test2.rb =3D " + DATA.readlines.to_s
__END__
This is the data in test2.rb.


Command session:

$ ruby test1.rb
test2.rb =3D This is the data in test1.rb.
test1.rb =3D=20
$=20


To say this is unexpected is putting it mildly. What I'd expect to see
is:

$ ruby test1.rb
test2.rb =3D This is the data in test1.rb.
test1.rb =3D=20
$=20


So two questions:

* Why is test2 getting the data from test1 and not from itself?
* How can get the behaviour I'm looking for?


--=20
Michael T. Richter <[email protected]> (GoogleTalk:
(e-mail address removed))
All really first class designers are both artists, engineers, and men of
a powerful and intolerant temper, quick to resist the least modification
of the plans, energetic in fighting the least infringement upon what
they regard as their own sphere of action. (Nevil Shute)

--=-0yx/9FOCCP88FVbBYwnC
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>
File: test1.rb:<BR>
<BLOCKQUOTE>
<TT>require 'test2'</TT><BR>
<TT>puts &quot;test1.rb =3D &quot; + DATA.readlines.to_s</TT><BR>
<TT>__END__</TT><BR>
<TT>This is the data in test1.rb.</TT><BR>
</BLOCKQUOTE>
<BR>
File: test2.rb:<BR>
<BLOCKQUOTE>
<TT>puts &quot;test2.rb =3D &quot; + DATA.readlines.to_s</TT><BR>
<TT>__END__</TT><BR>
<TT>This is the data in test2.rb.</TT><BR>
</BLOCKQUOTE>
<BR>
Command session:<BR>
<BLOCKQUOTE>
<TT>$ </TT><TT><B>ruby test1.rb</B></TT><BR>
<TT><I>test2.rb =3D This is the data in test1.rb.</I></TT><BR>
<TT><I>test1.rb =3D </I></TT><BR>
<TT>$ </TT><BR>
</BLOCKQUOTE>
<BR>
To say this is unexpected is putting it mildly.&nbsp; What I'd expect to se=
e is:<BR>
<BLOCKQUOTE>
<TT>$ </TT><TT><B>ruby test1.rb</B></TT><BR>
<TT><I>test2.rb =3D This is the data in test1.rb.</I></TT><BR>
<TT><I>test1.rb =3D </I></TT><BR>
<TT>$ </TT><BR>
</BLOCKQUOTE>
<BR>
So two questions:
<UL>
<LI>Why is test2 getting the data from test1 and not from itself?
<LI>How can get the behaviour I'm looking for?
</UL>
<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>All really first class designers are both artists, engineers, and men of=
a powerful and intolerant temper, quick to resist the least modification o=
f the plans, energetic in fighting the least infringement upon what they re=
gard as their own sphere of action. (Nevil Shute)</I>
</TD>
</TR>
</TABLE>
</BODY>
</HTML>

--=-0yx/9FOCCP88FVbBYwnC--

--=-M3ARuu4mBmyahaZUfWG5
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)

iD8DBQBG1/wOLqyWkKVQ54QRAqgNAKCDZTU8EFM4FN0I/JS0sDsm3K7kmwCfV6Sk
zzFKWlB/XRx3pL9qKFW6hlI=
=SgIE
-----END PGP SIGNATURE-----

--=-M3ARuu4mBmyahaZUfWG5--
 
M

Michael T. Richter

--=-f2GKtaxDNDEF1Imh1Jss
Content-Type: multipart/alternative; boundary="=-3a9W4ug1pKgf8l7HkxYY"


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

To say this is unexpected is putting it mildly. What I'd expect to
see is:
=20
$ ruby test1.rb
test2.rb =3D This is the data in test1.rb.
test1.rb =3D=20
$=20


*sigh* Of course what I really meant was I expected this:

test2.rb =3D This is the data in test2.rb.
test1.rb =3D This is the data in test1.rb


That'll teach me to post before proof-reading.

--=20
Michael T. Richter <[email protected]> (GoogleTalk:
(e-mail address removed))
A well-designed and humane interface does not need to be split into
beginner and expert subsystems. (Jef Raskin)

--=-3a9W4ug1pKgf8l7HkxYY
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-31-08 at 20:31 +0900, Michael T. Richter wrote:<BR>
<BLOCKQUOTE TYPE=3DCITE>
<FONT COLOR=3D"#000000">To say this is unexpected is putting it mildly.=
&nbsp; What I'd expect to see is:</FONT><BR>
<BLOCKQUOTE>
<TT><FONT COLOR=3D"#000000">$ </FONT></TT><TT><FONT COLOR=3D"#00000=
0"><B>ruby test1.rb</B></FONT></TT><BR>
<I><TT><FONT COLOR=3D"#000000">test2.rb =3D This is the data in tes=
t1.rb.</FONT></TT></I><BR>
<I><TT><FONT COLOR=3D"#000000">test1.rb =3D </FONT></TT></I><BR>
<TT><FONT COLOR=3D"#000000">$ </FONT></TT><BR>
</BLOCKQUOTE>
</BLOCKQUOTE>
<BR>
*sigh*&nbsp; Of course what I really meant was I expected this:<BR>
<BLOCKQUOTE>
<I><TT>test2.rb =3D This is the data in test2.rb.</TT></I><BR>
<I><TT>test1.rb =3D This is the data in test1.rb</TT></I><BR>
</BLOCKQUOTE>
<BR>
That'll teach me to post before proof-reading.<BR>
<TABLE CELLSPACING=3D"0" CELLPADDING=3D"0" WIDTH=3D"100%">
<TR>
<TD>
<BR>
</TD>
</TR>
</TABLE>
<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>A well-designed and humane interface does not need to be split into begi=
nner and expert subsystems. (Jef Raskin)</I>
</TD>
</TR>
</TABLE>
</BODY>
</HTML>

--=-3a9W4ug1pKgf8l7HkxYY--

--=-f2GKtaxDNDEF1Imh1Jss
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)

iD8DBQBG1/4dLqyWkKVQ54QRAio6AKC9eWktZQy2VG3s52xKpXRcBnli0wCfQNzh
RvsRyu6GmxvA/vRN9phDF5Y=
=EoOL
-----END PGP SIGNATURE-----

--=-f2GKtaxDNDEF1Imh1Jss--
 
F

Florian Aßmann

Hi Michael,

1. DATA *constant* becomes defined:
DATA.inspect # => "This is the data in test1.rb."
require 'test2'
*snip*
2. test2 becomes loaded
3. test2 reads lines from DATA(0) to DATA(EOF) and puts them
test2.rb = This is the data in test1.rb.
*snip*
4. test1 reads lines from DATA(EOF) to DATA(EOF) and puts them
puts "test1.rb = " + DATA.readlines.to_s

test1.rb =
__END__
This is the data in test1.rb.

Your dealing with a constant here, you should expect that it won't change. Ok,
you maybe expected that the require behaves like the ruby-bin...


Regards Florian
 
M

Michael T. Richter

--=-9+k0UV1R9uoYnKHQ4Jiq
Content-Type: multipart/alternative; boundary="=-UE+4vUcO9mnZH5Wtmqtf"


--=-UE+4vUcO9mnZH5Wtmqtf
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: quoted-printable

Hi Michael,
=20
1. DATA *constant* becomes defined:
DATA.inspect # =3D> "This is the data in test1.rb."
*snip*
2. test2 becomes loaded
3. test2 reads lines from DATA(0) to DATA(EOF) and puts them
=20
test2.rb =3D This is the data in test1.rb.
=20
*snip*
4. test1 reads lines from DATA(EOF) to DATA(EOF) and puts them
=20
test1.rb =3D
=20
=20
Your dealing with a constant here, you should expect that it won't change= Ok,
you maybe expected that the require behaves like the ruby-bin...


OK, that makes a twisted sort of sense, but... I *REALLY* do not like
this. At all. Not just because it royally screws what I'm working on
(although I may have a slightly clumsier workaround) but also because it
means any libraries I require in my code can suck up my __END__ data. I
would have expected the DATA constant to be localized in some way to the
language unit it's being used in, not passed around freely like some
kind of depraved party girl.

And what's the "ruby-bin"?

--=20
Michael T. Richter <[email protected]> (GoogleTalk:
(e-mail address removed))
All really first class designers are both artists, engineers, and men of
a powerful and intolerant temper, quick to resist the least modification
of the plans, energetic in fighting the least infringement upon what
they regard as their own sphere of action. (Nevil Shute)

--=-UE+4vUcO9mnZH5Wtmqtf
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-31-08 at 22:48 +0900, Florian Aßmann wrote:
<BLOCKQUOTE TYPE=3DCITE>
<PRE>
<FONT COLOR=3D"#000000">Hi Michael,</FONT>

<FONT COLOR=3D"#000000">1. DATA *constant* becomes defined:</FONT>
<FONT COLOR=3D"#000000">DATA.inspect # =3D&gt; &quot;This is the data in te=
st1.rb.&quot;</FONT>
<FONT COLOR=3D"#000000">&gt; require 'test2'</FONT>
<FONT COLOR=3D"#000000">*snip*</FONT>
<FONT COLOR=3D"#000000">2. test2 becomes loaded</FONT>
<FONT COLOR=3D"#000000">3. test2 reads lines from DATA(0) to DATA(EOF) and =
puts them</FONT>
<FONT COLOR=3D"#000000">&gt;&gt; puts &quot;test2.rb =3D &quot; + DATA.=
readlines.to_s</FONT>

<FONT COLOR=3D"#000000">test2.rb =3D This is the data in test1.rb.</FONT>

<FONT COLOR=3D"#000000">&gt;&gt; # ignored unless it's called first</FONT>
<FONT COLOR=3D"#000000">&gt;&gt; __END__</FONT>
<FONT COLOR=3D"#000000">&gt;&gt; This is the data in test2.rb.</FONT>
<FONT COLOR=3D"#000000">*snip*</FONT>
<FONT COLOR=3D"#000000">4. test1 reads lines from DATA(EOF) to DATA(EOF) an=
d puts them</FONT>
<FONT COLOR=3D"#000000">&gt; puts &quot;test1.rb =3D &quot; + DATA.read=
lines.to_s</FONT>

<FONT COLOR=3D"#000000">test1.rb =3D</FONT>

<FONT COLOR=3D"#000000">&gt; __END__</FONT>
<FONT COLOR=3D"#000000">&gt; This is the data in test1.rb.</FONT>

<FONT COLOR=3D"#000000">Your dealing with a constant here, you should expec=
t that it won't change. Ok,</FONT>
<FONT COLOR=3D"#000000">you maybe expected that the require behaves like th=
e ruby-bin...</FONT>
</PRE>
</BLOCKQUOTE>
<BR>
OK, that makes a twisted sort of sense, but...&nbsp; I *REALLY* do not like=
this.&nbsp; At all.&nbsp; Not just because it royally screws what I'm work=
ing on (although I may have a slightly clumsier workaround) but also becaus=
e it means any libraries I require in my code can suck up my __END__ data.&=
nbsp; I would have expected the DATA constant to be localized in some way t=
o the language unit it's being used in, not passed around freely like some =
kind of depraved party girl.<BR>
<BR>
And what's the &quot;ruby-bin&quot;?<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>All really first class designers are both artists, engineers, and men of=
a powerful and intolerant temper, quick to resist the least modification o=
f the plans, energetic in fighting the least infringement upon what they re=
gard as their own sphere of action. (Nevil Shute)</I>
</TD>
</TR>
</TABLE>
</BODY>
</HTML>

--=-UE+4vUcO9mnZH5Wtmqtf--

--=-9+k0UV1R9uoYnKHQ4Jiq
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)

iD8DBQBG2CMNLqyWkKVQ54QRAoMCAKCqAEo+EC6rtBStNSJl2AdYNA+FrQCgnGjq
ucoL4pWW9xXdK6roeskmaEM=
=P1mc
-----END PGP SIGNATURE-----

--=-9+k0UV1R9uoYnKHQ4Jiq--
 
F

Florian Aßmann

Hi Michael,

I once met this problem, too - but you know - you can always do for each file
you require:

module FileA
DATA = File.read __FILE__.gsub(/\.rb$/, '.txt')
end

Just put each const in it's own Namespace/Module and lessen your usage of
__END__. I always work in my own Namespaces, but not for each require though...
Naah, I mean the executable...

Regards
Florian
 

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

Similar Threads


Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top