Reading String Data as a File

R

Robert Klemme

R

Robert Klemme

2010/7/1 Robert Klemme said:
Perfect! =A0Do you think it is a good idea to also allow an IO as
argument to foreach so we can save a block?

FCSV($stdin) =A0 =A0 { |csv_in| =A0csv_in.each { |row| p row } } =A0# fro= m $stdin

would become

FCSV.foreach($stdin) =A0 =A0 { |row| p row } =A0# from $stdin

Btw, this inspired me to http://gist.github.com/459749

:)

Cheers

robert


--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 
J

James Edward Gray II

=20
Perfect! Do you think it is a good idea to also allow an IO as
argument to foreach so we can save a block?
=20
FCSV($stdin) { |csv_in| csv_in.each { |row| p row } } # from = $stdin
=20
would become
=20
FCSV.foreach($stdin) { |row| p row } # from $stdin

I would rather not go that far. The standard CSV library for Ruby 1.8 =
varied its interface slightly from the IO methods I assumed it meant to =
imitate. For example, open() was essentially foreach() when you passed =
an "r" mode. This always bothered me.

We don't need two blocks though. I showed it that way in the =
documentation for consistency (to hopefully make it easier to remember), =
but this works:

$ echo -e 'a,b,c' | ruby -rubygems -e 'require "faster_csv"; =
FCSV($stdin).each { |row| p row }'
["a", "b", "c"]

James Edward Gray II
 
R

Robert Klemme

Perfect! Do you think it is a good idea to also allow an IO as
argument to foreach so we can save a block?

FCSV($stdin) { |csv_in| csv_in.each { |row| p row } } # from
$stdin

would become

FCSV.foreach($stdin) { |row| p row } # from $stdin

I would rather not go that far. The standard CSV library for Ruby
1.8 varied its interface slightly from the IO methods I assumed it
meant to imitate. For example, open() was essentially foreach() when
you passed an "r" mode. This always bothered me.

We don't need two blocks though. I showed it that way in the
documentation for consistency (to hopefully make it easier to
remember), but this works:

$ echo -e 'a,b,c' | ruby -rubygems -e 'require "faster_csv";
FCSV($stdin).each { |row| p row }' ["a", "b", "c"]

Good point. Thank you for consideration of my suggestion.

Kind regards

robert
 

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

Members online

Forum statistics

Threads
474,148
Messages
2,570,838
Members
47,385
Latest member
Joneswilliam01

Latest Threads

Top