autochomp?

M

Martin DeMello

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
 
R

Robert Klemme

Martin DeMello said:
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
 
K

Kristof Bastiaensen

Hi,

Martin DeMello said:
[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
 
R

Robert Klemme

Kristof Bastiaensen said:
Hi,

Martin DeMello said:
[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
 
R

Robert Klemme

Martin DeMello said:
Robert Klemme said:
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
 
D

Dave Thomas

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


Cheers

Dave
 
R

Robert Klemme

Dave Thomas said:
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
 
D

Dave Thomas

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?

Cheers

Dave
 
J

Jamis Buck

Dave said:
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

--
Jamis Buck
(e-mail address removed)
http://www.jamisbuck.org/jamis

"I use octal until I get to 8, and then I switch to decimal."
 
A

Ara.T.Howard

Kristof Bastiaensen said:
Hi,

[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!).

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
--
===============================================================================
| 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
===============================================================================
 
Z

Zach Dennis

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?!!
 
H

Henrik Horneber

Stephan said:
Thanks. At least now I now what I'm suffering. ;-)



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

amazon.de says 1. November. Maybe preordering the book will scratch a
little of your itch? :)

Henrik
 
D

Dave Thomas

amazon.de says 1. November. Maybe preordering the book will scratch a
little of your itch? :)

If you really want it quickly, we'll be shipping it directly from here
once they come in. We're hoping to be able to ship internationally for
about $11, but depends on some packaging issues. We'll know next week.
We also hope to be able to take preorders within a week.

Cheers

Dave
 
J

James Britt

Henrik said:
amazon.de says 1. November. Maybe preordering the book will scratch a
little of your itch? :)

2.5 questions for Dave Thomas:

What's the fastest way for someone to get the book? Pre-order now from a
site such as Amazon, wait until the pragmatic programmer site allows
pre-orders, or some other option?

What's the best deal for you? Is it better if people order directly
from your site?


Thanks,


James
 
C

Curt Hibbs

Dave said:
If you really want it quickly, we'll be shipping it directly from here
once they come in. We're hoping to be able to ship internationally for
about $11, but depends on some packaging issues. We'll know next week.
We also hope to be able to take preorders within a week.

Great! Please let us know when you are taking preorders. I would much rather
order directly from you so that you don't have to split the money with the
middle-men.

Curt
 
P

Phil Tomson

amazon.de says 1. November. Maybe preordering the book will scratch a
little of your itch? :)

Wouldn't the Pragmatic Programmers get more money if we order it directly
from them? (also, I suspect they'll have copies sooner than Amazon).

Phil
 

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

No members online now.

Forum statistics

Threads
474,156
Messages
2,570,878
Members
47,413
Latest member
KeiraLight

Latest Threads

Top