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.
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.
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.
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
路路路
On Sunday 14 October 2007 09:42 pm, 7stud -- wrote:
Here are some other possibilities that might work for you depending on
what you're actually trying to accomplish:
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
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
路路路
On Monday 15 October 2007 12:59 pm, 7stud -- wrote:
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