Nested blocks?

R

Randy Kramer

The following works fine but I had some thoughts about trying to convert it to
two nested blocks (because of the extra goodness of blocks).

Can anybody tell me how to do that? I've been looking at the relevant pages
of the pickaxe (version 2) (around pages 49 thru 55), but that hasn't helped
me so far. ;-)

Is it worth doing?

g = File.open("test.aml", "w")
File.open("test.txt", "r").each { |line| g.puts line}
g.close

Randy Kramer

Aside: I suppose it's obvious, but basically the program copies test.txt to
test.aml one line at a time.
 
T

Tim Hunter

Randy said:
The following works fine but I had some thoughts about trying to convert it to
two nested blocks (because of the extra goodness of blocks).

Can anybody tell me how to do that? I've been looking at the relevant pages
of the pickaxe (version 2) (around pages 49 thru 55), but that hasn't helped
me so far. ;-)

Is it worth doing?

g = File.open("test.aml", "w")
File.open("test.txt", "r").each { |line| g.puts line}
g.close

Randy Kramer

Aside: I suppose it's obvious, but basically the program copies test.txt to
test.aml one line at a time.

I haven't tested this but I think it'll do the trick. Hmmm...this is
three nested blocks.

File.open("test.aml", "r") do |input|
File.open("test.txt", "w") do |output|
input.each {|line| output.puts line }
end
end
 
S

Stefano Crocco

Alle domenica 14 ottobre 2007, Randy Kramer ha scritto:
The following works fine but I had some thoughts about trying to convert it
to two nested blocks (because of the extra goodness of blocks).

Can anybody tell me how to do that? I've been looking at the relevant
pages of the pickaxe (version 2) (around pages 49 thru 55), but that hasn't
helped me so far. ;-)

Is it worth doing?

g = File.open("test.aml", "w")
File.open("test.txt", "r").each { |line| g.puts line}
g.close

Randy Kramer

Aside: I suppose it's obvious, but basically the program copies test.txt to
test.aml one line at a time.


File.open("test.aml", "w") do |f|
File.foreach("test.txt"){|l| f.write l}
end

I hope this helps

Stefano
 
R

Randy Kramer

File.open("test.aml", "w") do |f|
File.foreach("test.txt"){|l| f.write l}
end

I hope this helps

Stefano,

Yes, thank you! (And hopefully I'll internalize / remember this ;-)

Randy Kramer
 
R

Randy Kramer

I haven't tested this but I think it'll do the trick. Hmmm...this is
three nested blocks.

File.open("test.aml", "r") do |input|
File.open("test.txt", "w") do |output|
input.each {|line| output.puts line }
end
end

Thank you! It does work (well, I just had to interchange the two files, as
test.aml is my output).

Randy Kramer
 
7

7stud --

Randy said:
Is it worth doing?

g = File.open("test.aml", "w")
File.open("test.txt", "r").each { |line| g.puts line}
g.close

Aside: I suppose it's obvious, but basically the program copies test.txt
to
test.aml one line at a time.

Here are some other possibilities that might work for you depending on
what you're actually trying to accomplish:

1) File.rename('test.txt', 'test.aml')

2)
require 'fileutils'

FileUtils.copy('test.txt', 'test.aml')
 
R

Randy Kramer

Here are some other possibilities that might work for you depending on
what you're actually trying to accomplish:

1) File.rename('test.txt', 'test.aml')

2)
require 'fileutils'

FileUtils.copy('test.txt', 'test.aml')

Thanks, but the nested block is what I was looking for. What I'm writing is a
utility to convert one file format to another, and the business of copying
the file line by line is just a first iteration--eventually (well, I'm
working on that now) I'll add the code to detect certain lines or patterns
and convert them as necessary.

Irrelevant Aside: In some sense, I'm trying (or thinking I'm trying) part of
the extreme programming approach, sort of trying the simplest thing that
could possibly work. But, contrary to the way I've seen that concept
described, I'm biting off just a portion of the required functionality first,
i.e., copying one line at a time to the output file. Now I'm starting to add
the logic to examine and convert those lines, again, in little bite size
pieces (I hope. ;-)

Even More Irrelevant Aside: The thing that makes the job a little complicated
is that I can't simply convert line by line, in some cases I have to review
groups of 5 or more lines and, depending on what is there, convert those to a
smaller number of lines. But, I'm making progress.

Randy Kramer
 
7

7stud --

Randy said:
Even More Irrelevant Aside: The thing that makes the job a little
complicated
is that I can't simply convert line by line, in some cases I have to
review
groups of 5 or more lines and, depending on what is there, convert those
to a
smaller number of lines.

Have you seen each_slice() before?

require 'enumerator'

#create a file with some data:
File.open("data.txt", "w") do |file|
(1..22).each do |i|
file.puts("line #{i}")
end
end

#read the file in groups of 5 lines:
File.open("data.txt") do |file|
file.each_slice(5) do |lines|
p lines
end
end


--output:--
["line 1\n", "line 2\n", "line 3\n", "line 4\n", "line 5\n"]
["line 6\n", "line 7\n", "line 8\n", "line 9\n", "line 10\n"]
["line 11\n", "line 12\n", "line 13\n", "line 14\n", "line 15\n"]
["line 16\n", "line 17\n", "line 18\n", "line 19\n", "line 20\n"]
["line 21\n", "line 22\n"]
 
R

Randy Kramer

Have you seen each_slice() before?

Thanks! I've read (or skimmed, or attempted to read) enough Ruby books that
I'm fairly certain I must have seen it--recalling it is another thing. ;-)

BTW, I don't think it's applicable in my particular case--the situation is a
little more complex than I led you to believe. At some point I may be
posting my (almost) finished code for a round of constructive criticism, and
then you might be able to see what I mean.

In a brief attempt to give a hint:

2 (specific) lines in the input file become 3 in the output
3 (specific) lines in the input file become 3 in the output
4 (specific) lines in the input file can become 3 or 4 in the output
5 (specific) lines in the input file can become 3, 4, or 5 in the output
...
and other lines (not those specific lines) are simply copied from the input to
the output

Furthermore, those specific lines are of two different (general)
types--"general" intended to hint that the 2nd specific type of line comes in
two different subtypes. In addition, note that those specific lines contain
different text, i.e., they are titles for the records I'm processing.

Randy Kramer
require 'enumerator'

#create a file with some data:
File.open("data.txt", "w") do |file|
(1..22).each do |i|
file.puts("line #{i}")
end
end

#read the file in groups of 5 lines:
File.open("data.txt") do |file|
file.each_slice(5) do |lines|
p lines
end
end


--output:--
["line 1\n", "line 2\n", "line 3\n", "line 4\n", "line 5\n"]
["line 6\n", "line 7\n", "line 8\n", "line 9\n", "line 10\n"]
["line 11\n", "line 12\n", "line 13\n", "line 14\n", "line 15\n"]
["line 16\n", "line 17\n", "line 18\n", "line 19\n", "line 20\n"]
["line 21\n", "line 22\n"]
 

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
473,774
Messages
2,569,596
Members
45,142
Latest member
DewittMill
Top