Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialJessica Barnett
8,028 PointsRuby String.replace method
Why use this:
my_string.replace("new text")
When you could just reassign the variable like this:
my_string = "new text"
I was looking through the ruby docs to see if it doubles as a find-and-replace function like javascript's String.replace method, but it doesn't. There's another method, String.gsub, which fills that role.
Is there a performance benefit? I can't see any other reason why I should use it...
1 Answer
Michael Alaev
5,415 PointsEasier then declaring again variables down in your code for example in one function it can work as X and the other one it can work as Y. Also I think = works a bit slower then .replace but not sure.