Forum: Ruby Basic input/output question

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
B58c6eef325656d513d26e2c3ae6bfd9?d=identicon&s=25 Peter Bailey (peterbailey)
on 2006-04-06 16:36
Hello,
I spent my $40 on "Programming Ruby," but, it seems dedicated to C
programmer rocket scientists. I work in publishing and basically I just
need to open files, convert the data, and write the files, either to the
same file or to another. Can someone please help me to write a simple
structure to read a file "text1.txt", do regexes and stuff in it to
convert data, and then write the output to "text2.txt."

Thank you very much.
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2006-04-06 17:03
(Received via mailing list)
On Apr 6, 2006, at 9:36 AM, Peter Bailey wrote:

> Hello,
> I spent my $40 on "Programming Ruby," but, it seems dedicated to C
> programmer rocket scientists. I work in publishing and basically I
> just
> need to open files, convert the data, and write the files, either
> to the
> same file or to another. Can someone please help me to write a simple
> structure to read a file "text1.txt", do regexes and stuff in it to
> convert data, and then write the output to "text2.txt."

Sure, see if something like this would help you:

File.open("text2.txt", "w") do |output|
   File.foreach("text1.txt") do |line|
     # change line as need here, for example:
     # line.sub!(/regex here/, "replacement here")...

     output << line
   end
end

Hope that helps.

James Edward Gray II
801b207647d808477fc4514568c9d723?d=identicon&s=25 Dimitri Aivaliotis (Guest)
on 2006-04-06 17:06
(Received via mailing list)
Hi Peter,

On 4/6/06, Peter Bailey <pbailey@bna.com> wrote:
> Hello,
> I spent my $40 on "Programming Ruby," but, it seems dedicated to C
> programmer rocket scientists. I work in publishing and basically I just
> need to open files, convert the data, and write the files, either to the
> same file or to another. Can someone please help me to write a simple
> structure to read a file "text1.txt", do regexes and stuff in it to
> convert data, and then write the output to "text2.txt."

A very general way to do this is:

big = 10485760 # we'll deal with files larger than 10MB specially

read = "text1.txt" # the file to be read

write = "text2.txt" # the file to be written

File.open( write, "w") do |w|
  File.open( read ) do |r|
    if File.size( read ) < big
      while line = r.gets
	# do somethin with line
        w << line
      end
    else# read the file in big-sized chunks
      while !f.eof?
        f.read( big ).split($/).each do |line|
          # do somethin with line
          w << line
        end
      end
    end
  end
end

- Dimitri
E20e89d58211a3631842daecc1245de2?d=identicon&s=25 Ilmari Heikkinen (Guest)
on 2006-04-06 17:30
(Received via mailing list)
Hi,

On 4/6/06, Peter Bailey <pbailey@bna.com> wrote:
> Hello,
> I spent my $40 on "Programming Ruby," but, it seems dedicated to C
> programmer rocket scientists. I work in publishing and basically I just
> need to open files, convert the data, and write the files, either to the
> same file or to another. Can someone please help me to write a simple
> structure to read a file "text1.txt", do regexes and stuff in it to
> convert data, and then write the output to "text2.txt."
>
> Thank you very much.
>

If your files aren't very large, the simple way is to:

data = File.read("text1.txt")
result_data = do_stuff_to( data )
# open text2.txt in "w"rite mode
File.open("text2.txt", "w"){|text2|
  text2.write(result_data)
}
B58c6eef325656d513d26e2c3ae6bfd9?d=identicon&s=25 Peter Bailey (peterbailey)
on 2006-04-06 18:03
James Gray wrote:
> On Apr 6, 2006, at 9:36 AM, Peter Bailey wrote:
>
>> Hello,
>> I spent my $40 on "Programming Ruby," but, it seems dedicated to C
>> programmer rocket scientists. I work in publishing and basically I
>> just
>> need to open files, convert the data, and write the files, either
>> to the
>> same file or to another. Can someone please help me to write a simple
>> structure to read a file "text1.txt", do regexes and stuff in it to
>> convert data, and then write the output to "text2.txt."
>
> Sure, see if something like this would help you:
>
> File.open("text2.txt", "w") do |output|
>    File.foreach("text1.txt") do |line|
>      # change line as need here, for example:
>      # line.sub!(/regex here/, "replacement here")...
>
>      output << line
>    end
> end
>
> Hope that helps.
>
> James Edward Gray II

I think that this will help me a lot. Now, I see that this is line
oriented. Correct? So, line by line, I can read the lines, change stuff,
and write the lines. Cool. Thanks a lot, James!
B58c6eef325656d513d26e2c3ae6bfd9?d=identicon&s=25 Peter Bailey (peterbailey)
on 2006-04-06 18:06
Ilmari Heikkinen wrote:
> Hi,
>
> On 4/6/06, Peter Bailey <pbailey@bna.com> wrote:
>> Hello,
>> I spent my $40 on "Programming Ruby," but, it seems dedicated to C
>> programmer rocket scientists. I work in publishing and basically I just
>> need to open files, convert the data, and write the files, either to the
>> same file or to another. Can someone please help me to write a simple
>> structure to read a file "text1.txt", do regexes and stuff in it to
>> convert data, and then write the output to "text2.txt."
>>
>> Thank you very much.
>>
>
> If your files aren't very large, the simple way is to:
>
> data = File.read("text1.txt")
> result_data = do_stuff_to( data )
> # open text2.txt in "w"rite mode
> File.open("text2.txt", "w"){|text2|
>   text2.write(result_data)
> }

Thank you! This is great stuff, from all you guys. You're all heroes. .
. .
B58c6eef325656d513d26e2c3ae6bfd9?d=identicon&s=25 Peter Bailey (peterbailey)
on 2006-04-06 21:10
Dimitri Aivaliotis wrote:
> Hi Peter,
>
> On 4/6/06, Peter Bailey <pbailey@bna.com> wrote:
>> Hello,
>> I spent my $40 on "Programming Ruby," but, it seems dedicated to C
>> programmer rocket scientists. I work in publishing and basically I just
>> need to open files, convert the data, and write the files, either to the
>> same file or to another. Can someone please help me to write a simple
>> structure to read a file "text1.txt", do regexes and stuff in it to
>> convert data, and then write the output to "text2.txt."
>
> A very general way to do this is:
>
> big = 10485760 # we'll deal with files larger than 10MB specially
>
> read = "text1.txt" # the file to be read
>
> write = "text2.txt" # the file to be written
>
> File.open( write, "w") do |w|
>   File.open( read ) do |r|
>     if File.size( read ) < big
>       while line = r.gets
> 	# do somethin with line
>         w << line
>       end
>     else# read the file in big-sized chunks
>       while !f.eof?
>         f.read( big ).split($/).each do |line|
>           # do somethin with line
>           w << line
>         end
>       end
>     end
>   end
> end
>
> - Dimitri


Dimitri,
I kind of did what you had here, just a bit simpler:

Dir.chdir("C:/scripts/temp")
read    =   "eula.txt"
write   =   "test1.txt"
File.open(write, "w") do |w|
    File.open(read) do |r|
        while line = r.gets
        line.gsub(/Microsoft/, "Apple")
        w << line
        end
    end
end

It works. I get my file, "test1.txt," but, no changes were made to the
text. All the Microsofts are still Microsoft, and not Apple.

-Peter
801b207647d808477fc4514568c9d723?d=identicon&s=25 Dimitri Aivaliotis (Guest)
on 2006-04-06 21:19
(Received via mailing list)
Hi Peter,

On 4/6/06, Peter Bailey <pbailey@bna.com> wrote:

> I kind of did what you had here, just a bit simpler:

>         line.gsub(/Microsoft/, "Apple")

Here's where you need a ! -> that line should read:

           line.gsub!(/Microsoft/,"Apple")

(See James' example.)

The "gsub" will act on "line", and return a new object.  On the other
hand, the "gsub!" will change the "line" itself, without creating a
new object.  This is what you want in this case.

In general, when there's an exclamation point version of a method, it
will change the object itself, instead of returning a new, changed
object.

- Dimitri
B58c6eef325656d513d26e2c3ae6bfd9?d=identicon&s=25 Peter Bailey (peterbailey)
on 2006-04-06 22:21
Dimitri Aivaliotis wrote:
> Hi Peter,
>
> On 4/6/06, Peter Bailey <pbailey@bna.com> wrote:
>
>> I kind of did what you had here, just a bit simpler:
>
>>         line.gsub(/Microsoft/, "Apple")
>
> Here's where you need a ! -> that line should read:
>
>            line.gsub!(/Microsoft/,"Apple")
>
> (See James' example.)
>
> The "gsub" will act on "line", and return a new object.  On the other
> hand, the "gsub!" will change the "line" itself, without creating a
> new object.  This is what you want in this case.
>
> In general, when there's an exclamation point version of a method, it
> will change the object itself, instead of returning a new, changed
> object.
>
> - Dimitri

Cool. Thanks! So, in James' example, he uses "sub" instead of "gsub."
Are they the same thing?
801b207647d808477fc4514568c9d723?d=identicon&s=25 Dimitri Aivaliotis (Guest)
on 2006-04-06 22:38
(Received via mailing list)
On 4/6/06, Peter Bailey <pbailey@bna.com> wrote:

> Cool. Thanks! So, in James' example, he uses "sub" instead of "gsub."
> Are they the same thing?

"sub" will only replace the first occurrence of the pattern, whereas
"gsub" will replace all occurrences of the pattern in that string.

- Dimitri
B58c6eef325656d513d26e2c3ae6bfd9?d=identicon&s=25 Peter Bailey (peterbailey)
on 2006-04-07 13:27
Dimitri Aivaliotis wrote:
> On 4/6/06, Peter Bailey <pbailey@bna.com> wrote:
>
>> Cool. Thanks! So, in James' example, he uses "sub" instead of "gsub."
>> Are they the same thing?
>
> "sub" will only replace the first occurrence of the pattern, whereas
> "gsub" will replace all occurrences of the pattern in that string.
>
> - Dimitri

Thanks, Dimitri. Actually, I figured that out on the way home--the "sub"
versus "gsub." There are some things I have managed to get out my $40
book!
This topic is locked and can not be replied to.