Can I write to DATA?

Hi:

How would I modify the text below END?

----- begin file —

code to modify text below

END
change me
---- end file—

Thanks

···


Jim Freeze
If only I had something clever to say for my comment…
~

You mean like this?

f = open(FILE, “r+”)
if /(.?^END$.?)(^.+)/m =~ f.read
script_size = $1.size
f.pos = script_size
f.truncate(script_size)
f << $2.gsub(/^/, "> ") + "hello?\n"
end
f.close
END
hello?

···

At Fri, 23 Aug 2002 03:44:26 +0900, Jim Freeze wrote:

How would I modify the text below END?

----- begin file —

code to modify text below

END
change me
---- end file—

Jim Freeze wrote:

Hi:

How would I modify the text below END?

----- begin file —

code to modify text below

END
change me
---- end file—

Take a look at “roof” on RAA. It uses the DATA as a stream to keep
persistent state in.

(Briefly, roof is a simple oriented file system in which files are
treated as objects which have classes, methods, and state. The files are
executable, and methods are accessible as command-line arguments.)

See the documentation section on “Using the data section” and also the
datastream.rb file.

HTH,
Joel

Hi,

···

At Fri, 23 Aug 2002 05:44:07 +0900, GOTO Kentaro wrote:

How would I modify the text below END?

----- begin file —

code to modify text below

END
change me
---- end file—

You mean like this?

f = open(FILE, “r+”)
if /(.?^END$.?)(^.+)/m =~ f.read
script_size = $1.size
f.pos = script_size
f.truncate(script_size)
f << $2.gsub(/^/, "> ") + "hello?\n"
end
f.close
END
hello?

This fails for a script contains END inside string. I
recommend:

script_size = DATA.pos

And under Windows, I guess you need to close DATA before
reopen FILE.


Nobu Nakada

script_size = DATA.pos

The following script prints -1. Is there some other way to get the
position without parsing?

···

nobu.nokada@softhome.net wrote:


p DATA.pos
END
this is some data.

Here’s an example of the data stream approach.

require ‘roof/datastream’ # from Roof on RAA

DataStream.open($0, “r+”) do |data|
while line = data.gets and line != “END\n”
## maybe you can do this by seeking to DATA.pos,
## but that doesn’t seem to work in Ruby 1.6.7
end

data.substream do
# within this block, data behaves exactly as if everything before
# the current pos (which is just after END) was deleted.
data.print "Here is some data"
end
end

END

Hi,

···

At Fri, 23 Aug 2002 12:36:33 +0900, Joel VanderWerf wrote:

script_size = DATA.pos

The following script prints -1. Is there some other way to get the
position without parsing?


p DATA.pos
END
this is some data.

This code gave 19 for me. What’s your ruby version (and
release date) and platform?


Nobu Nakada

Joel VanderWerf wrote:

Here’s an example of the data stream approach.

I guess it should really be called “substream”. If there is interest, I
will check this in to RAA separately from Roof, and call it that.

Unless of course someone’s done something like this already… :slight_smile:

Here’s an example of the data stream approach.

require ‘roof/datastream’ # from Roof on RAA

DataStream.open($0, “r+”) do |data|
while line = data.gets and line != “END\n”
## maybe you can do this by seeking to DATA.pos,
## but that doesn’t seem to work in Ruby 1.6.7
end
DATA.pos works on 1.6.7 unix.

Jim

···

On Sun, Aug 25, 2002 at 07:31:13AM +0900, Joel VanderWerf wrote:

data.substream do
# within this block, data behaves exactly as if everything before
# the current pos (which is just after END) was deleted.
data.print "Here is some data"
end
end

END


Jim Freeze
If only I had something clever to say for my comment…
~

puts RUBY_VERSION # ==> 1.6.7
puts RUBY_PLATFORM # ==> i686-linux
puts RUBY_RELEASE_DATE # ==> 2002-03-01

#p DATA.type # ==> NameError

p DATA.pos # ==> -1

END
this is some data.

It looks like maybe DATA is in 1.7?

···

nobu.nokada@softhome.net wrote:

Hi,

At Fri, 23 Aug 2002 12:36:33 +0900, > Joel VanderWerf wrote:

script_size = DATA.pos

The following script prints -1. Is there some other way to get the
position without parsing?


p DATA.pos
END
this is some data.

This code gave 19 for me. What’s your ruby version (and
release date) and platform?

Jim Freeze wrote:

···

On Sun, Aug 25, 2002 at 07:31:13AM +0900, Joel VanderWerf wrote:

Here’s an example of the data stream approach.

require ‘roof/datastream’ # from Roof on RAA

DataStream.open($0, “r+”) do |data|
while line = data.gets and line != “END\n”
## maybe you can do this by seeking to DATA.pos,
## but that doesn’t seem to work in Ruby 1.6.7
end

DATA.pos works on 1.6.7 unix.

But does it return the position relative to the start of the file or
relative to the END? It seems to be the latter, so I can’t use
DATA.pos to skip to the data part. Nobu’s post suggests that there is
also a DATA in addition to DATA, but that doesn’t seem to exist in
1.6.7.

Hi,

script_size = DATA.pos

The following script prints -1. Is there some other way to get the
position without parsing?


p DATA.pos
END
this is some data.
This code gave 19 for me. What’s your ruby version (and
release date) and platform?

puts RUBY_VERSION # ==> 1.6.7
puts RUBY_PLATFORM # ==> i686-linux
puts RUBY_RELEASE_DATE # ==> 2002-03-01

All of last 1.4.6, 1.6.7 release and CVS latests of 1.6 and 1.7
worked properly.

It looks like maybe DATA is in 1.7?

Sorry, it’s a typo.

···

At Sun, 25 Aug 2002 02:21:51 +0900, Joel VanderWerf wrote:


Nobu Nakada

It returns the position relative to the start of the file.
So,
File.open(FILE,“w”) { |f|
f.pos = DATA.pos
f.puts “write over the data after END
}
will start writing on the lone following END.

···

On Sun, Aug 25, 2002 at 08:56:57AM +0900, Joel VanderWerf wrote:

Jim Freeze wrote:

On Sun, Aug 25, 2002 at 07:31:13AM +0900, Joel VanderWerf wrote:

Here’s an example of the data stream approach.

require ‘roof/datastream’ # from Roof on RAA

DataStream.open($0, “r+”) do |data|
while line = data.gets and line != “END\n”
## maybe you can do this by seeking to DATA.pos,
## but that doesn’t seem to work in Ruby 1.6.7
end

DATA.pos works on 1.6.7 unix.

But does it return the position relative to the start of the file or
relative to the END? It seems to be the latter, so I can’t use
DATA.pos to skip to the data part. Nobu’s post suggests that there is
also a DATA in addition to DATA, but that doesn’t seem to exist in
1.6.7.


Jim Freeze
If only I had something clever to say for my comment…
~

Well, you’re right. It works from the command line. I was running it
from nedit, which for some reason is confused (like me :).

···

nobu.nokada@softhome.net wrote:

Hi,

At Sun, 25 Aug 2002 02:21:51 +0900, > Joel VanderWerf wrote:

script_size = DATA.pos

The following script prints -1. Is there some other way to get the
position without parsing?


p DATA.pos
END
this is some data.

This code gave 19 for me. What’s your ruby version (and
release date) and platform?