[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

how to stop gsub from returning nil

Tom Cloyd

6/16/2008 7:58:00 AM

I'm trying to use gsub to do a number of transformations in an array of
strings. I find that when a particular transformation does NOT happen,
because the searched-for substring is not there, gsub returns nil. This
effectively ruins my output. I don't want nothing. I want the string
that's being processed, returned with or without any transformations. Is
there any alternative to testing for a return of nil before calling
gsub, so as to avoid the wiping out of my string? I've looked for
something to use other than String::gsub, and have not found anything.

Code:

filein = open( "{whatever}" )
fi = filein.readlines
delta = [ ["</p>", ''], ["</h1>", ''] ]
results = fi.collect do |x|
delta.each do |y|
debugger
x.gsub!(y[0], y[1])
end
end

t.

--

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Tom Cloyd, MS MA, LMHC
Private practice Psychotherapist
Bellingham, Washington, U.S.A: (360) 920-1226
<< tc@tomcloyd.com >> (email)
<< TomCloyd.com >> (website & psychotherapy weblog)
<< sleightmind.wordpress.com >> (mental health issues weblog)
<< directpathdesign.com >> (web site design & consultation)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


9 Answers

Peña, Botp

6/16/2008 8:08:00 AM

0

From: Tom Cloyd [mailto:tomcloyd@comcast.net]=20
# x.gsub!(y[0], y[1])

try,

x=3Dx.gsub(y[0], y[1])

kind regards -botp

Mikael Høilund

6/16/2008 8:42:00 AM

0


On Jun 16, 2008, at 9:58, Tom Cloyd wrote:

> I want the string that's being processed, returned with or without
> any transformations.


If you want to fall-back on something when something returns nil (or
false), use the || operator:

debugger x.gsub!(y[0], y[1]) || x

--
a,b=%Q=Z,O^NPO\r4_PV\\PI\x15^-\x0\v=,email=%\%%%c\%115%%# Mikael
Hoilund, CTO
okay=%#;hmm=(0...a.size).map{|i|((a[i]-email[i]+2)%128).# of Meta.io
ApS from
chr}.join;!email.gsub!'o',"%c%c"%[3+?0.<<(2),?G.~@];aha=#############
Denmark
hmm.scan(/#{'(.)'*5}/);!puts(email[1..-12]+aha.shift.zip(*aha).join)#
Ruby <3


Robert Klemme

6/16/2008 9:11:00 AM

0

2008/6/16 Tom Cloyd <tomcloyd@comcast.net>:
> I'm trying to use gsub to do a number of transformations in an array of
> strings. I find that when a particular transformation does NOT happen,
> because the searched-for substring is not there, gsub returns nil. This
> effectively ruins my output. I don't want nothing. I want the string that's
> being processed, returned with or without any transformations. Is there any
> alternative to testing for a return of nil before calling gsub, so as to
> avoid the wiping out of my string? I've looked for something to use other
> than String::gsub, and have not found anything.

You first need to decide whether you want to do all your
transformations in place (i.e. on the original strings in the Array)
or whether you need a copy of all strings - with or without changes.

> Code:
>
> filein = open( "{whatever}" )
> fi = filein.readlines
> delta = [ ["</p>", ''], ["</h1>", ''] ]
> results = fi.collect do |x|
> delta.each do |y|
> debugger x.gsub!(y[0], y[1])
> end
> end

It's not clear what you intend to do with results, but I assume for
the moment that you need copies. In that case you probably should not
use String#gsub! but String#gsub (i.e. the version which leaves the
original untouched).

Few other remarks:

- You do not use the block form of file opening and thus you leave the
file descriptor open which is bad.

- You can read a complete file as Array via File.readlines("whatever")

- You can read a complete file as String via File.read("whatever")

- a Hash seems more appropriate for delta because it nicely expresses
the key value relationship between search criteria and replacement
string and also prevents accidental duplicates. Downside is that you
loose order if that is important for you.

- Reading the file as single String might be more efficient because in
that case you only need one gsub per replacement expression

So, here's probably what I'd do

delta = { %r{</(?:p|h1)>}i => '' }

c = File.read "whatever.html"
delta.each do |rx, repl|
c.gsub! rx, repl
end

puts c

Kind regards

robert

--
use.inject do |as, often| as.you_can - without end

Tom Cloyd

6/16/2008 9:54:00 AM

0

Thank you all for your kind help. Very much appreciated!

I've basically got my little HTML to textile program working well enough
to use. I really didn't have much time to do it, and your help saved my
bacon.

Comments below...

Robert Klemme wrote:
> 2008/6/16 Tom Cloyd <tomcloyd@comcast.net>:
>
>> I'm trying to use gsub to do a number of transformations in an array of
>> strings. I find that when a particular transformation does NOT happen,
>> because the searched-for substring is not there, gsub returns nil. This
>> effectively ruins my output. I don't want nothing. I want the string that's
>> being processed, returned with or without any transformations. Is there any
>> alternative to testing for a return of nil before calling gsub, so as to
>> avoid the wiping out of my string? I've looked for something to use other
>> than String::gsub, and have not found anything.
>>
>
> You first need to decide whether you want to do all your
> transformations in place (i.e. on the original strings in the Array)
> or whether you need a copy of all strings - with or without changes.
>
>
I ended up doing them in place. Couldn't figure a clean way to make
copies, then feed them back into the implicit loop.
>> Code:
>>
>> filein = open( "{whatever}" )
>> fi = filein.readlines
>> delta = [ ["</p>", ''], ["</h1>", ''] ]
>> results = fi.collect do |x|
>> delta.each do |y|
>> debugger x.gsub!(y[0], y[1])
>> end
>> end
>>
>
> It's not clear what you intend to do with results, but I assume for
> the moment that you need copies. In that case you probably should not
> use String#gsub! but String#gsub (i.e. the version which leaves the
> original untouched).
>
My current code ("fi" is an input textfile - an array of strings):

fi.collect do |x|
delta.each do |y| # <= this is as I specified before, but much larger
if nil!=x.gsub!(y[0], y[1]) then # <= this makes no obvious
difference in processing time
x.gsub!(y[0], y[1])
end
end
fileout.write x
end
> Few other remarks:
>
> - You do not use the block form of file opening and thus you leave the
> file descriptor open which is bad.
>
Erg. Got me.
> - You can read a complete file as Array via File.readlines("whatever")
>
> - You can read a complete file as String via File.read("whatever")
>
I used the first, but wasn't clear about the last. Thanks.
> - a Hash seems more appropriate for delta because it nicely expresses
> the key value relationship between search criteria and replacement
> string and also prevents accidental duplicates. Downside is that you
> loose order if that is important for you.
>
Yeah, and that downside would be fatal. Order matters. Array it must be.
> - Reading the file as single String might be more efficient because in
> that case you only need one gsub per replacement expression
>
Oh. Now that's slick. Many thanks.
> So, here's probably what I'd do
>
> delta = { %r{</(?:p|h1)>}i => '' }
>
> c = File.read "whatever.html"
> delta.each do |rx, repl|
> c.gsub! rx, repl
> end
>
> puts c
>
Well, I need to study that. Amazing terse. But, then that's the miracle
of Ruby, isn't it.
Almost poetry.

Thanks again!

t.

--

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Tom Cloyd, MS MA, LMHC
Private practice Psychotherapist
Bellingham, Washington, U.S.A: (360) 920-1226
<< tc@tomcloyd.com >> (email)
<< TomCloyd.com >> (website & psychotherapy weblog)
<< sleightmind.wordpress.com >> (mental health issues weblog)
<< directpathdesign.com >> (web site design & consultation)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


Robert Dober

6/16/2008 10:23:00 AM

0

On Mon, Jun 16, 2008 at 9:58 AM, Tom Cloyd <tomcloyd@comcast.net> wrote:
> I'm trying to use gsub to do a number of transformations in an array of
> strings. I find that when a particular transformation does NOT happen,
> because the searched-for substring is not there, gsub returns nil. This
> effectively ruins my output. I don't want nothing. I want the string that's
> being processed, returned with or without any transformations. Is there any
> alternative to testing for a return of nil before calling gsub, so as to
> avoid the wiping out of my string? I've looked for something to use other
> than String::gsub, and have not found anything.
>
> Code:
>
> filein = open( "{whatever}" )
> fi = filein.readlines
> delta = [ ["</p>", ''], ["</h1>", ''] ]
> results = fi.collect do |x|
> delta.each do |y|
> debugger x.gsub!(y[0], y[1])
> end
> end
>
> t.
>
> --
>
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> Tom Cloyd, MS MA, LMHC
> Private practice Psychotherapist
> Bellingham, Washington, U.S.A: (360) 920-1226
> << tc@tomcloyd.com >> (email)
> << TomCloyd.com >> (website & psychotherapy weblog)
> << sleightmind.wordpress.com >> (mental health issues weblog)
> << directpathdesign.com >> (web site design & consultation)
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
>
>



Why not write a simple wrapper?

Ruby 1.9
class String
def mysub! *args, &blk
tap{ gsub!( *args, &blk ) }
end
end

Ruby 1.8 or 1.9 too ;)
...
def mysub!...
gsub!....
self
end


Cheers
Robert
--
http://ruby-smalltalk.blo...

---
As simple as possible, but not simpler.
Albert Einstein

Tom Cloyd

6/16/2008 10:39:00 AM

0

Robert Dober wrote:
> On Mon, Jun 16, 2008 at 9:58 AM, Tom Cloyd <tomcloyd@comcast.net> wrote:
>
>> I'm trying to use gsub to do a number of transformations in an array of
>> strings. I find that when a particular transformation does NOT happen,
>> because the searched-for substring is not there, gsub returns nil. This
>> effectively ruins my output. I don't want nothing. I want the string that's
>> being processed, returned with or without any transformations. Is there any
>> alternative to testing for a return of nil before calling gsub, so as to
>> avoid the wiping out of my string? I've looked for something to use other
>> than String::gsub, and have not found anything.
>>
>> Code:
>>
>> filein = open( "{whatever}" )
>> fi = filein.readlines
>> delta = [ ["</p>", ''], ["</h1>", ''] ]
>> results = fi.collect do |x|
>> delta.each do |y|
>> debugger x.gsub!(y[0], y[1])
>> end
>> end
>>
>> t.
>>
>> --
>>
>> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>> Tom Cloyd, MS MA, LMHC
>> Private practice Psychotherapist
>> Bellingham, Washington, U.S.A: (360) 920-1226
>> << tc@tomcloyd.com >> (email)
>> << TomCloyd.com >> (website & psychotherapy weblog)
>> << sleightmind.wordpress.com >> (mental health issues weblog)
>> << directpathdesign.com >> (web site design & consultation)
>> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>>
>>
>>
>>
>
>
>
> Why not write a simple wrapper?
>
> Ruby 1.9
> class String
> def mysub! *args, &blk
> tap{ gsub!( *args, &blk ) }
> end
> end
>
> Ruby 1.8 or 1.9 too ;)
> ...
> def mysub!...
> gsub!....
> self
> end
>
>
> Cheers
> Robert
>
Simple answer: I don't know the idiom. I have no idea what you just
said. None. Wrappers in my world are for candy. (!) I haven't caught up
yet with your *last* code, but I'm working on it. Then I'll go to work
on THIS. I much appreciate the learning challenges you're sending my
way. Hope some others are benefiting as well.

Thanks!

t.

--

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Tom Cloyd, MS MA, LMHC
Private practice Psychotherapist
Bellingham, Washington, U.S.A: (360) 920-1226
<< tc@tomcloyd.com >> (email)
<< TomCloyd.com >> (website & psychotherapy weblog)
<< sleightmind.wordpress.com >> (mental health issues weblog)
<< directpathdesign.com >> (web site design & consultation)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


Robert Dober

6/16/2008 11:53:00 AM

0

On Mon, Jun 16, 2008 at 12:39 PM, Tom Cloyd <tomcloyd@comcast.net> wrote:
> Robert Dober wrote:
>>
>> On Mon, Jun 16, 2008 at 9:58 AM, Tom Cloyd <tomcloyd@comcast.net> wrote:
>>
>>>
>>> I'm trying to use gsub to do a number of transformations in an array of
>>> strings. I find that when a particular transformation does NOT happen,
>>> because the searched-for substring is not there, gsub returns nil. This
>>> effectively ruins my output. I don't want nothing. I want the string
>>> that's
>>> being processed, returned with or without any transformations. Is there
>>> any
>>> alternative to testing for a return of nil before calling gsub, so as to
>>> avoid the wiping out of my string? I've looked for something to use other
>>> than String::gsub, and have not found anything.
>>>
>>> Code:
>>>
>>> filein = open( "{whatever}" )
>>> fi = filein.readlines
>>> delta = [ ["</p>", ''], ["</h1>", ''] ]
>>> results = fi.collect do |x|
>>> delta.each do |y|
>>> debugger x.gsub!(y[0], y[1])
>>> end
>>> end
>>>
>>> t.
>>>
>>> --
>>>
>>> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>>> Tom Cloyd, MS MA, LMHC
>>> Private practice Psychotherapist
>>> Bellingham, Washington, U.S.A: (360) 920-1226
>>> << tc@tomcloyd.com >> (email)
>>> << TomCloyd.com >> (website & psychotherapy weblog)
>>> << sleightmind.wordpress.com >> (mental health issues weblog)
>>> << directpathdesign.com >> (web site design & consultation)
>>> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>>>
>>>
>>>
>>>
>>
>>
>>
>> Why not write a simple wrapper?
>>
>> Ruby 1.9
>> class String
>> def mysub! *args, &blk
>> tap{ gsub!( *args, &blk ) }
>> end
>> end
>>
>> Ruby 1.8 or 1.9 too ;)
>> ...
>> def mysub!...
>> gsub!....
>> self
>> end
>>
>>
>> Cheers
>> Robert
>>
>
> Simple answer: I don't know the idiom.
wrapper means the same as with candy, leave gsub! to do the task and
wrap mysub! around to return
self as gsub! does not.
The first idiom is from RUby1.9 I 'll give you the simple example
again, it is easier to understand

class String
def mysub! *args, &blk
gsub! *args, &blk # we still want the basic gsub! behavior, so
we just let it do its work!
self # aditionally we return the (potentially modified) string
you were missing so badly ;)
end
end

Quite simple, no? If not Albert will not be happy ;).

HTH
Robert
--
http://ruby-smalltalk.blo...

---
As simple as possible, but not simpler.
Albert Einstein

pjb

6/16/2008 11:59:00 AM

0

Tom Cloyd <tomcloyd@comcast.net> writes:

> Robert Dober wrote:
>> Why not write a simple wrapper?
>>
>> Ruby 1.9
>> class String
>> def mysub! *args, &blk
>> tap{ gsub!( *args, &blk ) }
>> end
>> end
>>
>> Ruby 1.8 or 1.9 too ;)
>> ...
>> def mysub!...
>> gsub!....
>> self
>> end
>>
>>
>> Cheers
>> Robert
>>
> Simple answer: I don't know the idiom. I have no idea what you just
> said. None. Wrappers in my world are for candy. (!) I haven't caught up
> yet with your *last* code, but I'm working on it. Then I'll go to work
> on THIS. I much appreciate the learning challenges you're sending my
> way. Hope some others are benefiting as well.

It's the same as with candy. If you don't like the color of your
candy, you take a paper of some other color, and wrap it over the
candy. If you still don't like the pattern of this layer, you add
another wrapper with a better pattern, and so on.

So instead of touching/looking at the candy, you just touch and look
the wrapper.

Instead of using gsub, which you don't like, use your wrapper method.


--
__Pascal Bourguignon__

Michael Steinfeld

6/16/2008 5:44:00 PM

0

On Mon, Jun 16, 2008 at 7:59 AM, Pascal J. Bourguignon
<pjb@informatimago.com> wrote:
> Tom Cloyd <tomcloyd@comcast.net> writes:
>
>> Robert Dober wrote:
>>> Why not write a simple wrapper?
>>>
>>> Ruby 1.9
>>> class String
>>> def mysub! *args, &blk
>>> tap{ gsub!( *args, &blk ) }
>>> end
>>> end
>>>
>>> Ruby 1.8 or 1.9 too ;)
>>> ...
>>> def mysub!...
>>> gsub!....
>>> self
>>> end
>>>
>>>
>>> Cheers
>>> Robert
>>>
>> Simple answer: I don't know the idiom. I have no idea what you just
>> said. None. Wrappers in my world are for candy. (!) I haven't caught up
>> yet with your *last* code, but I'm working on it. Then I'll go to work
>> on THIS. I much appreciate the learning challenges you're sending my
>> way. Hope some others are benefiting as well.
>
> It's the same as with candy. If you don't like the color of your
> candy, you take a paper of some other color, and wrap it over the
> candy. If you still don't like the pattern of this layer, you add
> another wrapper with a better pattern, and so on.
>
> So instead of touching/looking at the candy, you just touch and look
> the wrapper.
>
> Instead of using gsub, which you don't like, use your wrapper method.
>
>
> --
> __Pascal Bourguignon__
>
>

you can always do something simple like:

#{row.foo ? row.foo.gsub(%r{[',()\\\/]},"") : "Dont Be Nil, be somthing else"}