Multiple gsub statements in one line possible?

K

Kurt Euler

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
 
H

Hal Fulton

Kurt said:
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
 
G

Gavin Sinclair

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
 
M

Mauricio Fernández

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)

and even

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. :)
-- Goswin Brederlow
 

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,121
Messages
2,570,712
Members
47,282
Latest member
hopkins1988

Latest Threads

Top