[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

chop problems

BearItAll

2/3/2005 10:31:00 AM

Hello, I'm new here, also fairly new to ruby, but I already have some
very usfull scripts written with it.

But a problem I have over and over is that the 'chop' doesn't always
work (for me) when dealing with files.

An example, the file '/tmp/dirtree' is the result of a class that
traverses the directory tree, the lines are written using,

f.print "#{filename}\n"
(but I've also tried others writing methods)

Then next would be the class to read from that file.

DIRTREE='/tmp/dirtree'
IO.foreach(DIRTREE) { |@x|
@x.chop
print "Thisdir = #{@x}/*"
}

The output from print gives,

Thisdir = /home/whatever
/*

So chop hasn't removed the newline.

Do you use a definitive combination of file write and read that will
always work, or is there another trick I don't know about yet.

PS: I' on UNIX/Linux.

Thankyou
7 Answers

Robert Klemme

2/3/2005 10:49:00 AM

0


"BearItAll" <spam@rassler.co.uk> schrieb im Newsbeitrag
news:1107426590.1467.0@eunomia.uk.clara.net...
> Hello, I'm new here, also fairly new to ruby, but I already have some
> very usfull scripts written with it.
>
> But a problem I have over and over is that the 'chop' doesn't always
> work (for me) when dealing with files.
>
> An example, the file '/tmp/dirtree' is the result of a class that
> traverses the directory tree, the lines are written using,
>
> f.print "#{filename}\n"
> (but I've also tried others writing methods)

"f.puts filename" does the same more efficiently.

> Then next would be the class to read from that file.
>
> DIRTREE='/tmp/dirtree'
> IO.foreach(DIRTREE) { |@x|
> @x.chop
> print "Thisdir = #{@x}/*"
> }
>
> The output from print gives,
>
> Thisdir = /home/whatever
> /*
>
> So chop hasn't removed the newline.

You want chop!() because you want to modify @x. chop() just returns a
copy of the string with the last char removed. But even better use
chomp!(), because chop!() and chop() always remove the last char -
regardless what it is:

>> s="abcde\n"
=> "abcde\n"
>> s.chop!
=> "abcde"
>> s.chop!
=> "abcd"
>> s.chop!
=> "abc"
>> s.chop!
=> "ab"
>> s.chop!
=> "a"
>> s.chop!
=> ""
>> s.chop!
=> nil

> Do you use a definitive combination of file write and read that will
> always work, or is there another trick I don't know about yet.

Kind regards

robert

BearItAll

2/3/2005 12:04:00 PM

0

Robert Klemme wrote:
> "BearItAll" <spam@rassler.co.uk> schrieb im Newsbeitrag
> news:1107426590.1467.0@eunomia.uk.clara.net...
>
>>Hello, I'm new here, also fairly new to ruby, but I already have some
>>very usfull scripts written with it.
>>
>>But a problem I have over and over is that the 'chop' doesn't always
>>work (for me) when dealing with files.
>>
>>An example, the file '/tmp/dirtree' is the result of a class that
>>traverses the directory tree, the lines are written using,
>>
>>f.print "#{filename}\n"
>>(but I've also tried others writing methods)
>
>
> "f.puts filename" does the same more efficiently.
>
>
>>Then next would be the class to read from that file.
>>
>>DIRTREE='/tmp/dirtree'
>>IO.foreach(DIRTREE) { |@x|
>> @x.chop
>> print "Thisdir = #{@x}/*"
>>}
>>
>>The output from print gives,
>>
>>Thisdir = /home/whatever
>>/*
>>
>>So chop hasn't removed the newline.
>
>
> You want chop!() because you want to modify @x. chop() just returns a
> copy of the string with the last char removed. But even better use
> chomp!(), because chop!() and chop() always remove the last char -
> regardless what it is:
>
>
>>>s="abcde\n"
>
> => "abcde\n"
>
>>>s.chop!
>
> => "abcde"
>
>>>s.chop!
>
> => "abcd"
>
>>>s.chop!
>
> => "abc"
>
>>>s.chop!
>
> => "ab"
>
>>>s.chop!
>
> => "a"
>
>>>s.chop!
>
> => ""
>
>>>s.chop!
>
> => nil
>
>
>>Do you use a definitive combination of file write and read that will
>>always work, or is there another trick I don't know about yet.
>
>
> Kind regards
>
> robert
>

Thankyou, I love you loads.

Strange really how I read that "Returns a new string..." a billion
times, without actually noticing what it said.

But then I'm the same with tax return forms.

So as a reward you are welcome to join me in a all expenses paid round
the world holiday on my yaght. it's about 3 foot long and 2 foot wide
with one oar each. Let me know when your avaiable.

Robert Klemme

2/3/2005 12:19:00 PM

0


"BearItAll" <spam@rassler.co.uk> schrieb im Newsbeitrag
news:1107432176.47652.0@demeter.uk.clara.net...
> Robert Klemme wrote:
> > "BearItAll" <spam@rassler.co.uk> schrieb im Newsbeitrag
> > news:1107426590.1467.0@eunomia.uk.clara.net...
> >
> >>Hello, I'm new here, also fairly new to ruby, but I already have some
> >>very usfull scripts written with it.
> >>
> >>But a problem I have over and over is that the 'chop' doesn't always
> >>work (for me) when dealing with files.
> >>
> >>An example, the file '/tmp/dirtree' is the result of a class that
> >>traverses the directory tree, the lines are written using,
> >>
> >>f.print "#{filename}\n"
> >>(but I've also tried others writing methods)
> >
> >
> > "f.puts filename" does the same more efficiently.
> >
> >
> >>Then next would be the class to read from that file.
> >>
> >>DIRTREE='/tmp/dirtree'
> >>IO.foreach(DIRTREE) { |@x|
> >> @x.chop
> >> print "Thisdir = #{@x}/*"
> >>}
> >>
> >>The output from print gives,
> >>
> >>Thisdir = /home/whatever
> >>/*
> >>
> >>So chop hasn't removed the newline.
> >
> >
> > You want chop!() because you want to modify @x. chop() just returns a
> > copy of the string with the last char removed. But even better use
> > chomp!(), because chop!() and chop() always remove the last char -
> > regardless what it is:
> >
> >
> >>>s="abcde\n"
> >
> > => "abcde\n"
> >
> >>>s.chop!
> >
> > => "abcde"
> >
> >>>s.chop!
> >
> > => "abcd"
> >
> >>>s.chop!
> >
> > => "abc"
> >
> >>>s.chop!
> >
> > => "ab"
> >
> >>>s.chop!
> >
> > => "a"
> >
> >>>s.chop!
> >
> > => ""
> >
> >>>s.chop!
> >
> > => nil
> >
> >
> >>Do you use a definitive combination of file write and read that will
> >>always work, or is there another trick I don't know about yet.
> >
> >
> > Kind regards
> >
> > robert
> >
>
> Thankyou, I love you loads.

*blush*

> Strange really how I read that "Returns a new string..." a billion
> times, without actually noticing what it said.
>
> But then I'm the same with tax return forms.

So you never get money back?

> So as a reward you are welcome to join me in a all expenses paid round
> the world holiday on my yaght. it's about 3 foot long and 2 foot wide
> with one oar each. Let me know when your avaiable.

Lemmesee, I think, before I can join this invitation I'll have to undergo
some shrinking - otherwise there'll be hardly enough space on your
"ship"...

:-)

robert

Pit Capitain

2/3/2005 12:46:00 PM

0

Robert Klemme schrieb:
> "BearItAll" <spam@rassler.co.uk> schrieb im Newsbeitrag
> news:1107432176.47652.0@demeter.uk.clara.net...
>>So as a reward you are welcome to join me in a all expenses paid round
>>the world holiday on my yaght. it's about 3 foot long and 2 foot wide
>>with one oar each. Let me know when your avaiable.
>
> Lemmesee, I think, before I can join this invitation I'll have to undergo
> some shrinking - otherwise there'll be hardly enough space on your
> "ship"...

Should be easy:

robert.chop!

Regards,
Pit


Robert Klemme

2/3/2005 1:38:00 PM

0


"Pit Capitain" <pit@capitain.de> schrieb im Newsbeitrag
news:42021CFA.7000006@capitain.de...
> Robert Klemme schrieb:
> > "BearItAll" <spam@rassler.co.uk> schrieb im Newsbeitrag
> > news:1107432176.47652.0@demeter.uk.clara.net...
> >>So as a reward you are welcome to join me in a all expenses paid round
> >>the world holiday on my yaght. it's about 3 foot long and 2 foot wide
> >>with one oar each. Let me know when your avaiable.
> >
> > Lemmesee, I think, before I can join this invitation I'll have to
undergo
> > some shrinking - otherwise there'll be hardly enough space on your
> > "ship"...
>
> Should be easy:
>
> robert.chop!

Dar, didn' thin tha i wa s eas.

Cheer

rober

Caio Tiago Oliveira

2/3/2005 4:24:00 PM

0

BearItAll, 3/2/2005 09:05:

> Strange really how I read that "Returns a new string..." a billion
> times, without actually noticing what it said.

@x = @x.chop
is the same as
@x.chop!

@x.chop return a new string you can assing to a variable you want to.



Robert Klemme

2/3/2005 4:51:00 PM

0


"Caio Tiago Oliveira" <caiot1@ibest.com.br> schrieb im Newsbeitrag
news:42025196.2050502@ibest.com.br...
> BearItAll, 3/2/2005 09:05:
>
> > Strange really how I read that "Returns a new string..." a billion
> > times, without actually noticing what it said.
>
> @x = @x.chop
> is the same as
> @x.chop!

No, it is not. In many applications this might be equivalent but it's not
the same. @x.chop! does not create a new instance, i.e. whoever holds a
reference to that instance sees the change. @x=@x.chop creates a new
instance so everybody else referencing the old value of @x sees no change.
Also, it makes a difference when you consider #freeze:

>> s = "abcd"
=> "abcd"
>> s.freeze
=> "abcd"
>> s.chop!
TypeError: can't modify frozen string
from (irb):3:in `chop!'
from (irb):3
>> s = s.chop
=> "abc"

Also, it makes a difference performance wise. The first form is usually
slower since a new instance has to be created.

> @x.chop return a new string you can assing to a variable you want to.

Yes.

Kind regards

robert