class String
def capitalize_at(*indices)
str = self.dup
indices.each do |i|
if chr = self[i, 1]
str[i,1] = chr.upcase
else
raise IndexError.new("character #{i} out of index")
end
end
str
end
end
"helloha".capitalize_at(0, 3) # => "HelLoha"
If you find yourself doing it frequently.