[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Weird problem with DATA and __END__

Michael T. Richter

8/31/2007 11:32:00 AM

File: test1.rb:

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


File: test2.rb:

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


Command session:

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


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

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


So two questions:

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


--
Michael T. Richter <ttmrichter@gmail.com> (GoogleTalk:
ttmrichter@gmail.com)
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)
4 Answers

Michael T. Richter

8/31/2007 11:41:00 AM

0

On Fri, 2007-31-08 at 20:31 +0900, Michael T. Richter wrote:

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


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

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


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

--
Michael T. Richter <ttmrichter@gmail.com> (GoogleTalk:
ttmrichter@gmail.com)
A well-designed and humane interface does not need to be split into
beginner and expert subsystems. (Jef Raskin)

Florian Aßmann

8/31/2007 1:49:00 PM

0

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
>> puts "test2.rb = " + DATA.readlines.to_s

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

>> # ignored unless it's called first
>> __END__
>> This is the data in test2.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

Michael T. Richter

8/31/2007 2:18:00 PM

0

On Fri, 2007-31-08 at 22:48 +0900, Florian Aßmann wrote:

> 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
> >> puts "test2.rb = " + DATA.readlines.to_s
>
> test2.rb = This is the data in test1.rb.
>
> >> # ignored unless it's called first
> >> __END__
> >> This is the data in test2.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...


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"?

--
Michael T. Richter <ttmrichter@gmail.com> (GoogleTalk:
ttmrichter@gmail.com)
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)

Florian Aßmann

8/31/2007 2:48:00 PM

0

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

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

> >
> > And what's the "ruby-bin"?
Naah, I mean the executable...

Regards
Florian