Nested blocks?

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

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.

Randy Kramer wrote:

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

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

路路路

--
RMagick OS X Installer [http://rubyforge.org/projects/rmagick/\]
RMagick Hints & Tips [http://rubyforge.org/forum/forum.php?forum_id=1618\]
RMagick Installation FAQ [http://rmagick.rubyforge.org/install-faq.html\]

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

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

Randy Kramer wrote:

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')

路路路

--
Posted via http://www.ruby-forum.com/\.

Stefano,

Yes, thank you! (And hopefully I'll internalize / remember this :wink:

Randy Kramer

路路路

On Sunday 14 October 2007 04:36 pm, Stefano Crocco wrote:

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

I hope this helps

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

Randy Kramer

路路路

On Sunday 14 October 2007 04:03 pm, Tim Hunter wrote:

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

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

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:

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

2)
require 'fileutils'

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

Randy Kramer wrote:

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

路路路

--
Posted via http://www.ruby-forum.com/\.

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

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

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

--
Posted via http://www.ruby-forum.com/\.