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!
even a different approach, that would be great!
Well, that code should work to replace the first underscore in a
String. If you want all of them, use gsub!() instead. You can also
drop the if statement, as it has no effect here.
I would use tr() for this, if you want to replace them all:
Coming recently from Perl, this looks very familiar to me. In Perl
syntax:
$cal_name =~ tr/_/ /;
In Ruby you’ll need cal_name.tr!("", " “) if you want to make the
substitution permanent. (I keep getting caught by this!) Or
cal_name.tr_s!(”", " ") if you want to squeeze multiple space
characters down to a single space too.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.