开发者

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" 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜