Any way to make IO.readlines chomp each line before adding it to the
array? (i.e. a = IO.readlines.map {|i| i.chomp} but done in the
readlines call itself)
martin
Any way to make IO.readlines chomp each line before adding it to the
array? (i.e. a = IO.readlines.map {|i| i.chomp} but done in the
readlines call itself)
martin
"Martin DeMello" <martindemello@yahoo.com> schrieb im Newsbeitrag
news:idIZc.301315$J06.4478@pd7tw2no...
Any way to make IO.readlines chomp each line before adding it to the
array? (i.e. a = IO.readlines.map {|i| i.chomp} but done in the
readlines call itself)
Not AFAIK. You can do
content = IO.readlines(file).each {|l| l.chomp!}
or (more efficient)
content = File.open(file) {|io| io.inject() {|ar,line| line.chomp!; ar <<
line} }
But I guess you figured that yourself already.
Kind regards
robert
Hi,
"Martin DeMello" <martindemello@yahoo.com> schrieb im Newsbeitrag
news:idIZc.301315$J06.4478@pd7tw2no...[quoted text muted]
Not AFAIK. You can do
content = IO.readlines(file).each {|l| l.chomp!}
or (more efficient)
content = File.open(file) {|io| io.inject() {|ar,line| line.chomp!; ar
<< line} }
I think in this case the first is more efficient than the second.
Since chomp! modifies the object inplace, no object gets created.
Regards,
Kristof
On Thu, 02 Sep 2004 19:18:10 +0200, Robert Klemme wrote:
But I guess you figured that yourself already.
Kind regards
robert
[...]
Ah - I was hoping there was some sort of $variable that controlled
chomping.
martin
Robert Klemme <bob.news@gmx.net> wrote:
"Martin DeMello" <martindemello@yahoo.com> schrieb im Newsbeitrag
news:idIZc.301315$J06.4478@pd7tw2no...
> Any way to make IO.readlines chomp each line before adding it to the
> array? (i.e. a = IO.readlines.map {|i| i.chomp} but done in the
> readlines call itself)Not AFAIK. You can do
"Kristof Bastiaensen" <kristof@vleeuwen.org> schrieb im Newsbeitrag
news:pan.2004.09.02.20.13.44.101750@vleeuwen.org...
Hi,
>
> "Martin DeMello" <martindemello@yahoo.com> schrieb im Newsbeitrag
> news:idIZc.301315$J06.4478@pd7tw2no...
>> [quoted text muted]
>
> Not AFAIK. You can do
>
> content = IO.readlines(file).each {|l| l.chomp!}
>
> or (more efficient)
>
> content = File.open(file) {|io| io.inject() {|ar,line| line.chomp!; ar
> << line} }
>I think in this case the first is more efficient than the second.
Since chomp! modifies the object inplace, no object gets created.
??? Please read again. The first is less efficient because it iterates
through the lines of the file twice and both use inplace modification
(#chomp!).
Kind regards
robert
On Thu, 02 Sep 2004 19:18:10 +0200, Robert Klemme wrote:
"Martin DeMello" <martindemello@yahoo.com> schrieb im Newsbeitrag
news:%cTZc.292866$M95.74360@pd7tw1no...
Robert Klemme <bob.news@gmx.net> wrote:
>
> "Martin DeMello" <martindemello@yahoo.com> schrieb im Newsbeitrag
> news:idIZc.301315$J06.4478@pd7tw2no...
> > Any way to make IO.readlines chomp each line before adding it to the
> > array? (i.e. a = IO.readlines.map {|i| i.chomp} but done in the
> > readlines call itself)
>
> Not AFAIK. You can do[...]
Ah - I was hoping there was some sort of $variable that controlled
chomping.
If at all a method argument acting as flag would be appropriate ($global ==
EVIL). I guess it was omitted because #readlines has already an optional
parameter (the line terminator).
Kind regards
robert
What we need is TRAITS! Let's implement Perl6 in Ruby
Cheers
Dave
On Sep 3, 2004, at 11:10, Robert Klemme wrote:
If at all a method argument acting as flag would be appropriate ($global ==
EVIL). I guess it was omitted because #readlines has already an optional
parameter (the line terminator).
what's wrong with IO.foreach?
lines =
IO.foreach(file){|line| lines << line.chomp!}
or if you have to have one line
IO.foreach(file){|line| (lines||=) << line.chomp!}
short, effecient, already built-in - what's not to like
i like dave's idea of adding traits to ruby though, if we start now maybe one
day ruby will be as good as perl6. i hear it's almost done now!
-a
On Fri, 3 Sep 2004, Robert Klemme wrote:
"Kristof Bastiaensen" <kristof@vleeuwen.org> schrieb im Newsbeitrag
news:pan.2004.09.02.20.13.44.101750@vleeuwen.org...Hi,
On Thu, 02 Sep 2004 19:18:10 +0200, Robert Klemme wrote:
"Martin DeMello" <martindemello@yahoo.com> schrieb im Newsbeitrag
news:idIZc.301315$J06.4478@pd7tw2no...[quoted text muted]
Not AFAIK. You can do
content = IO.readlines(file).each {|l| l.chomp!}
or (more efficient)
content = File.open(file) {|io| io.inject() {|ar,line| line.chomp!; ar
<< line} }I think in this case the first is more efficient than the second.
Since chomp! modifies the object inplace, no object gets created.??? Please read again. The first is less efficient because it iterates
through the lines of the file twice and both use inplace modification
(#chomp!).
EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
PHONE :: 303.497.6469
A flower falls, even though we love it;
and a weed grows, even though we do not love it. --Dogen
===============================================================================
Hi,
"Kristof Bastiaensen" <kristof@vleeuwen.org> schrieb im Newsbeitrag
news:pan.2004.09.02.20.13.44.101750@vleeuwen.org...Hi,
> "Martin DeMello" <martindemello@yahoo.com> schrieb im Newsbeitrag
> news:idIZc.301315$J06.4478@pd7tw2no...
>> [quoted text muted]
>
> Not AFAIK. You can do
>
> content = IO.readlines(file).each {|l| l.chomp!}
>
> or (more efficient)
>
> content = File.open(file) {|io| io.inject() {|ar,line| line.chomp!;
> ar << line} }
>
>
I think in this case the first is more efficient than the second. Since
chomp! modifies the object inplace, no object gets created.??? Please read again. The first is less efficient because it iterates
through the lines of the file twice and both use inplace modification
(#chomp!).Kind regards
robert
It would seem logical, but benchmarking shows different:
#---- begin filetest.rb ----
require 'benchmark'
file = "testfile.txt"
Benchmark.bm(7) do |x|
x.report("readlines:") do
IO.readlines(file).each {|l| l.chomp!}
end
x.report("inject:") do
File.open(file) {|io| io.inject() {
>ar,line| line.chomp!; ar << line}}
end
x.report("foreach:") do
IO.foreach(file){|line| (lines||=) << line.chomp!}
end
end
#---- end filetest.rb -----
$ seq 10000 > testfile.txt
$ ruby filetest.rb
readlines: 0.050000 0.010000 0.060000 ( 0.085483)
inject: 0.130000 0.010000 0.140000 ( 0.172144)
foreach: 0.080000 0.000000 0.080000 ( 0.112690)
Regards,
KB
On Fri, 03 Sep 2004 18:03:35 +0200, Robert Klemme wrote:
On Thu, 02 Sep 2004 19:18:10 +0200, Robert Klemme wrote:
"Dave Thomas" <dave@pragprog.com> schrieb im Newsbeitrag
news:6D03438C-FDC4-11D8-9F8D-000A95676A62@pragprog.com...
> If at all a method argument acting as flag would be appropriate
> ($global ==
> EVIL). I guess it was omitted because #readlines has already an
> optional
> parameter (the line terminator).What we need is TRAITS! Let's implement Perl6 in Ruby
As I'm ignorant of Perl 6, how would you solve this by using traits?
Wouldn't a second method be simpler? (IO#readcontent or such)
Kind regards
robert
On Sep 3, 2004, at 11:10, Robert Klemme wrote:
I have been suffering from what appears to be an pre-PickAxe 2 Suspense Disorder (PP2SD?) ever since the Pickaxe 2 announcement was made. I know this is an OT post, but I figure the ruby list can be my support group.
Dear Dave,
I just don't know if I can wait until October!! What is a rubyist to do! I think cloning Dave Thomas and Andy Hunt may be for the better of society.
Sincerecly,
Zach
Does anyone else feel this amount of suspense besides me?!!
Dave Thomas ha scritto:
What we need is TRAITS! Let's implement Perl6 in Ruby
wow, I got the idea that traits were some kind of mixin
"Kristof Bastiaensen" <kristof@vleeuwen.org> schrieb im Newsbeitrag
news:pan.2004.09.03.22.50.31.89211@vleeuwen.org...
Hi,
>
> "Kristof Bastiaensen" <kristof@vleeuwen.org> schrieb im Newsbeitrag
> news:pan.2004.09.02.20.13.44.101750@vleeuwen.org...
>> Hi,
>>
>>
>>
>> > "Martin DeMello" <martindemello@yahoo.com> schrieb im Newsbeitrag
>> > news:idIZc.301315$J06.4478@pd7tw2no...
>> >> [quoted text muted]
>> >
>> > Not AFAIK. You can do
>> >
>> > content = IO.readlines(file).each {|l| l.chomp!}
>> >
>> > or (more efficient)
>> >
>> > content = File.open(file) {|io| io.inject() {|ar,line| line.chomp!;
>> > ar << line} }
>> >
>> >
>> I think in this case the first is more efficient than the second. Since
>> chomp! modifies the object inplace, no object gets created.
>
> ??? Please read again. The first is less efficient because it iterates
> through the lines of the file twice and both use inplace modification
> (#chomp!).
>
> Kind regards
>
> robertIt would seem logical, but benchmarking shows different:
#---- begin filetest.rb ----
require 'benchmark'file = "testfile.txt"
Benchmark.bm(7) do |x|
x.report("readlines:") do
IO.readlines(file).each {|l| l.chomp!}
endx.report("inject:") do
File.open(file) {|io| io.inject() {
>ar,line| line.chomp!; ar << line}}
endx.report("foreach:") do
IO.foreach(file){|line| (lines||=) << line.chomp!}
end
end
#---- end filetest.rb -----$ seq 10000 > testfile.txt
$ ruby filetest.rb
readlines: 0.050000 0.010000 0.060000 ( 0.085483)
inject: 0.130000 0.010000 0.140000 ( 0.172144)
foreach: 0.080000 0.000000 0.080000 ( 0.112690)
That's really interesting. Did you also test with a real world file (a file
with longer lines presumably)? Interestingly enough, changing the block of
#inject does make it nearly as fast as readlines:
require 'benchmark'
file = "testfile.txt"
Benchmark.bm(7) do |x|
x.report("readlines:") do
IO.readlines(file).each {|l| l.chomp!}
end
x.report("inject:") do
File.open(file) {|io| io.inject() {
>ar,line| line.chomp!; ar << line}}
end
x.report("inject2:") do
File.open(file) {|io| io.inject() {
>ar,line| ar << line.chomp!}}
end
x.report("foreach:") do
IO.foreach(file){|line| (lines||=) << line.chomp!}
end
x.report("foreach2:") do
lines =
IO.foreach(file){|line| lines << line.chomp!}
end
end
$ seq 10000 > testfile.txt
$ ruby filetest.rb
user system total real
readlines: 0.078000 0.000000 0.078000 ( 0.073000)
inject: 0.094000 0.015000 0.109000 ( 0.097000)
inject2: 0.047000 0.016000 0.063000 ( 0.072000)
foreach: 0.109000 0.000000 0.109000 ( 0.100000)
foreach2: 0.047000 0.000000 0.047000 ( 0.048000)
And with more lines:
$ seq 1000000 >| testfile.txt
$ ruby filetest.rb
user system total real
readlines: 5.625000 0.250000 5.875000 ( 5.907000)
inject: 13.985000 0.719000 14.704000 ( 20.221000)
inject2: 13.797000 0.046000 13.843000 ( 14.308000)
foreach: 13.000000 0.032000 13.032000 ( 13.408000)
foreach2: 7.281000 0.031000 7.312000 ( 7.455000)
Kind regards
robert
On Fri, 03 Sep 2004 18:03:35 +0200, Robert Klemme wrote:
>> On Thu, 02 Sep 2004 19:18:10 +0200, Robert Klemme wrote:
Why add just a single method when we can add an ENTIRE NEW FEATURE SET and complicate the language at the same time?
Cheers
Dave
On Sep 3, 2004, at 11:45, Robert Klemme wrote:
What we need is TRAITS! Let's implement Perl6 in Ruby
As I'm ignorant of Perl 6, how would you solve this by using traits?
Wouldn't a second method be simpler? (IO#readcontent or such)
Zach Dennis wrote:
I have been suffering from what appears to be an pre-PickAxe 2 Suspense Disorder (PP2SD?) ever since the Pickaxe 2 announcement was made. I know this is an OT post, but I figure the ruby list can be my support group.
Have you see this page?
It has previews of certain chapters.
James
Robert Klemme ha scritto:
That's really interesting. Did you also test with a real world file (a file
with longer lines presumably)? Interestingly enough, changing the block of
#inject does make it nearly as fast as readlines:
did you (both) also considered that the undelying OS' caching strategy may have some effect on this results?
"Robert Klemme" <bob.news@gmx.net> writes:
"Kristof Bastiaensen" <kristof@vleeuwen.org> schrieb im Newsbeitrag
news:pan.2004.09.03.22.50.31.89211@vleeuwen.org...Hi,
>
> "Kristof Bastiaensen" <kristof@vleeuwen.org> schrieb im Newsbeitrag
> news:pan.2004.09.02.20.13.44.101750@vleeuwen.org...
>> Hi,
>>
>>
>>
>> > "Martin DeMello" <martindemello@yahoo.com> schrieb im Newsbeitrag
>> > news:idIZc.301315$J06.4478@pd7tw2no...
>> >> [quoted text muted]
>> >
>> > Not AFAIK. You can do
>> >
>> > content = IO.readlines(file).each {|l| l.chomp!}
>> >
>> > or (more efficient)
>> >
>> > content = File.open(file) {|io| io.inject() {|ar,line| line.chomp!;
>> > ar << line} }
>> >
>> >
>> I think in this case the first is more efficient than the second. Since
>> chomp! modifies the object inplace, no object gets created.
>
> ??? Please read again. The first is less efficient because it iterates
> through the lines of the file twice and both use inplace modification
> (#chomp!).
>
> Kind regards
>
> robertIt would seem logical, but benchmarking shows different:
#---- begin filetest.rb ----
require 'benchmark'file = "testfile.txt"
Benchmark.bm(7) do |x|
x.report("readlines:") do
IO.readlines(file).each {|l| l.chomp!}
endx.report("inject:") do
File.open(file) {|io| io.inject() {
>ar,line| line.chomp!; ar << line}}
endx.report("foreach:") do
IO.foreach(file){|line| (lines||=) << line.chomp!}
end
end
#---- end filetest.rb -----$ seq 10000 > testfile.txt
$ ruby filetest.rb
readlines: 0.050000 0.010000 0.060000 ( 0.085483)
inject: 0.130000 0.010000 0.140000 ( 0.172144)
foreach: 0.080000 0.000000 0.080000 ( 0.112690)That's really interesting. Did you also test with a real world file (a file
with longer lines presumably)? Interestingly enough, changing the block of
#inject does make it nearly as fast as readlines:
What's the interesting bit?
"readlines" is probably quicker than "inject" since there are less
method calls. In "inject" and "foreach" (and "inject2" and "foreach2"
in your later post), there are two method calls in the block: chomp!
and <<. In "readlines" there's only 1. All that pushing and popping
of ruby stack frames, method name looking up... it all adds up, more
so, evidently, than walking the array an extra time.
(Also note that "foreach" won't work right if chomp! returns nil, which
it will on the last line if there's no newline @ EOF.)
On Fri, 03 Sep 2004 18:03:35 +0200, Robert Klemme wrote:
>> On Thu, 02 Sep 2004 19:18:10 +0200, Robert Klemme wrote:
Dave Thomas wrote:
What we need is TRAITS! Let's implement Perl6 in Ruby
As I'm ignorant of Perl 6, how would you solve this by using traits?
Wouldn't a second method be simpler? (IO#readcontent or such)Why add just a single method when we can add an ENTIRE NEW FEATURE SET and complicate the language at the same time?
Is it just me, or do I detect a hint of bitterness there, Dave?
- Jamis
On Sep 3, 2004, at 11:45, Robert Klemme wrote:
--
Jamis Buck
jgb3@email.byu.edu
http://www.jamisbuck.org/jamis
"I use octal until I get to 8, and then I switch to decimal."
James Britt wrote:
Zach Dennis wrote:
I have been suffering from what appears to be an pre-PickAxe 2 Suspense Disorder (PP2SD?) ever since the Pickaxe 2 announcement was
Thanks. At least now I now what I'm suffering.
made. I know this is an OT post, but I figure the ruby list can be my support group.
Have you see this page?
https://pragprog.com/titles/ruby/programming-ruby-2nd-edition/It has previews of certain chapters.
....and it just made it worse. No! Don't remove that PDFs.
Happy rubying
Stephan ...wondering how long it will take to get the book over here in Germany
--
Stephan Kämper/IT-Beratung http://www.stephankaemper.de
Quality Assurance / Software Test / Data Analysis
"Dave Thomas" <dave@pragprog.com> schrieb im Newsbeitrag
news:A72DF7B2-FDC9-11D8-9F8D-000A95676A62@pragprog.com...
>> What we need is TRAITS! Let's implement Perl6 in Ruby
>
> As I'm ignorant of Perl 6, how would you solve this by using traits?
> Wouldn't a second method be simpler? (IO#readcontent or such)Why add just a single method when we can add an ENTIRE NEW FEATURE SET
and complicate the language at the same time?
Ah well, yes of course! Ruby is just too simple.
robert
On Sep 3, 2004, at 11:45, Robert Klemme wrote: