Multiple gsub statements in one line possible?

All-

For the string called pkg_bug_base, I’m currently using the following code to make some substitutions:

pkg_bug_base.gsub!(/<sp_num>/, sp_num_string)
pkg_bug_base.gsub!(/<rel_num>/, rel_num)

Question: Is there a way to put this all on one line? I tried this, but the code crashed here:

pkg_bug_base.gsub!(/<sp_num>/, sp_num_string).gsub!(/<rel_num>/, rel_num)

Thanks.

-ke

Kurt Euler wrote:

All-

For the string called pkg_bug_base, I’m currently using the following code to make some substitutions:

pkg_bug_base.gsub!(/<sp_num>/, sp_num_string)
pkg_bug_base.gsub!(/<rel_num>/, rel_num)

Question: Is there a way to put this all on one line? I tried this, but the code crashed here:

pkg_bug_base.gsub!(/<sp_num>/, sp_num_string).gsub!(/<rel_num>/, rel_num)

What probably happened s that the first gsub! didn’t actually change
anything. In that case, it returns nil. This is a feature that
allows you to test whether anything was changed. Unfortunately, it
also prevents safe chaining.

I’ll bet your error was that nil doesn’t have a gsub! method or
something like that. If so, this is what happened.

Hal

Forgive me for stating the obvious… Do you have to do in-place gsub? Can
you get away with this?:

pkg_bug_base = pkg_bug_base.gsub(/<sp_num>/,
sp_num_string).gsub(/<rel_num>/, rel_num)

“Kurt Euler” keuler@portal.com wrote in message
news:C47CCC6238EFD4119C5200508B95A1000C1A5C1D@cup1ex1.portal.com

All-

For the string called pkg_bug_base, I’m currently using the following code
to make some substitutions:

pkg_bug_base.gsub!(/<sp_num>/, sp_num_string)
pkg_bug_base.gsub!(/<rel_num>/, rel_num)

Question: Is there a way to put this all on one line? I tried this, but
the code crashed here:

pkg_bug_base.gsub!(/<sp_num>/, sp_num_string).gsub!(/<rel_num>/,
rel_num)

···

Thanks.

-ke

All-

For the string called pkg_bug_base, I’m currently using the following
code to make some substitutions:

pkg_bug_base.gsub!(/<sp_num>/, sp_num_string)
pkg_bug_base.gsub!(/<rel_num>/, rel_num)

Question: Is there a way to put this all on one line? I tried this, but
the code crashed here:

pkg_bug_base.gsub!(/<sp_num>/, sp_num_string).gsub!(/<rel_num>/,
rel_num)

Further to Hal’s response, if you remove the !s then everything will be OK.

I suggest you run ‘ri gsub!’ to see what it is that Hal’s talking about –
assuming you have ri installed.

If a method returns nil, as “gsub!(/<sp_num>/, sp_num_string)” is liable
to, then all further operations on it are likely to fail. For the sake of
a bit of fun, you could do this:

class NilClass
def gsub!(*args)
raise “Bad luck, buddy”
end
end

Not that I suggest that sort of thing for real code.

Gavin

and even

pkg_bug_base = pkg_bug_base.gsub(/<sp_num>/,
sp_num_string).gsub!(/<rel_num>/, rel_num)

···

On Thu, Sep 04, 2003 at 03:04:58PM +0900, Joe Cheng wrote:

Forgive me for stating the obvious… Do you have to do in-place gsub? Can
you get away with this?:

pkg_bug_base = pkg_bug_base.gsub(/<sp_num>/,
sp_num_string).gsub(/<rel_num>/, rel_num)


_ _

__ __ | | ___ _ __ ___ __ _ _ __
'_ \ / | __/ __| '_ _ \ / ` | ’ \
) | (| | |
__ \ | | | | | (| | | | |
.__/ _,
|_|/| || ||_,|| |_|
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

Debian is like Suse with yast turned off, just better. :slight_smile:
– Goswin Brederlow