Why is there a # replace line in Ruby?

While looking at the ruby ​​documentation, I found the replace method , but I cannot figure out what might be a use case for this method.

The only thing I can think of is memory management (something like reallocation if a new line has a length less than or equal to the previous one).

Any ideas?

+5
source share
2 answers

A practical example is simple if you want to achieve something like cross-referencing in other languages, where a variable value can be changed directly. That way you can pass a String to a method, and this method can completely change the string to something else.

, , String . (. " " ).

, , , , , (, , ).

+3

, , replace:

myString = "Welcome to PHP!"

= > " PHP!"

myString.replace "Goodbye to PHP!"

= > " PHP!"

- http://www.techotopia.com/index.php/Ruby_String_Replacement,_Substitution_and_Insertion#Changing_a_Section_of_a_String

+2

All Articles