Skip to content
Tags

,

gsub! returns nil when there is no match

September 16, 2011
ruby-1.9.2-p290 :044 > "game".gsub("game", "tame")
=> "tame"
ruby-1.9.2-p290 :045 > "game".gsub!("game", "tame")
=> "tame"
ruby-1.9.2-p290 :046 > "home".gsub("game", "tame")
=> "home"
ruby-1.9.2-p290 :048 > "home".gsub!("game", "tame")
=> nil

This is probably so that you use it in a conditional with the !, it modified in place so you wouldn’t use it with an assignment so you could do:

if “game”.gsub!(“game”, “tame”)

more like,

if some_variable.gsub!(“game”, “tame”)

Advertisements

From → Uncategorized

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: