From: Robert Klemme Date: 2008-06-11T03:29:52+09:00 Subject: Re: replacing a character in a string On 10.06.2008 20:19, Amanda .. wrote: > So I want to be able to take a string with underscores in it ('_') and > replace those with spaces...can somebody please tell me why this isn't > working: > > if cal_name.include? '_' > cal_name.sub!('_', ' ') > end > > I can't see why this isn't doing the trick...but maybe I'm doing > something wrong. If anyone could give me any idea of what's wrong..or > even a different approach, that would be great! Works for me - but if you use sub instead of gsub only the first occurrence is replaced. irb(main):001:0> s="a_b" => "a_b" irb(main):002:0> s.include? "_" => true irb(main):003:0> s.sub '_', ' ' => "a b" irb(main):004:0> s="a_b_c" => "a_b_c" irb(main):005:0> s.sub '_', ' ' => "a b_c" irb(main):006:0> I'd do just this: irb(main):006:0> s.gsub '_', ' ' => "a b c" Or, in your case use gsub! - but without the check (unnecessary overhead). Kind regards robert