I want to replace a character in string when a particular condition is satisfied. So , I went through the API doc of Ruby and found the gsub , gsub! etc for similar purpose. When I implemented that in my program I didn't got any error but din't got the desired output too.
The code which I was trying is this :
name.each_char { |c|
if name[c] == "a"
name.sub( name[c] , c )
puts "matched.... "
end
So , for example , I have a string called huzefa and want to replace all the letters with its index numbers . So , what is the way to do it ? Please explain in detail by giving a simple example.
downvotemight have triggered/influenced the second downvote. (I didnot downvote though, rather upvoted)len = 'huzefa'.size; (0...len).map(&:to_s).join # "012345". The result is the same for every other six-character string.