i’m trying to loop through a Ruby string containing many lines using the each_line method, but I also want to change them. I’m using the following code, but it doesn’t seem to work:
string.each_line{|line| line=change_line(line)}
I suppose, that Ruby is sending a copy of my line and not the line itself, but unfortunatelly there is no method each_line!. I also tried with the gsub! method, using /^.*$/ to detect each line, but it seems that it calls the change_line method only ones and replaces all lines with it. Any ideas how to do that?
Thanks in advance 🙂
You should try starting out with a blank string too, each_lining through the string and then pushing the results onto the blank string.
In your original example, you are correct. Your changes are occuring but they are not being ssved anywhere.
Eachin Ruby does not alter anything by default.