How to optimize 'string.split("&").sort.join("&")' using Ruby on Rails?
I am using Ruby on Rails 3 and in my code I have this:
string = "surname=testsurname&name=testname"
开发者_如何学Gostring.split("&").sort.join("&")
# Now the 'string' value is "name=testname&surname=testsurname"
There is a better way to do that?
If by better you mean faster, then probably not. That's a fairly straightforward implementation of what you're intending to do. How often are you calling this method? What context is it being called in? To optimize this you would probably look at ways to avoid performing this operation more times than is strictly required.
for better understanding you can change your code a little bit
string = "surname = testsurname & name = testname".split(" & ").sort().join(" & ")
=> "name = testname & surname = testsurname"
精彩评论