more ruby way of gsub from array
Posted
by aharon
on Stack Overflow
See other posts from Stack Overflow
or by aharon
Published on 2010-06-07T16:02:49Z
Indexed on
2010/06/07
16:22 UTC
Read the original article
Hit count: 287
ruby
My goal is to let there be x
so that x("? world. what ? you say...", ['hello', 'do'])
returns "hello world. what do you say..."
.
I have something that works, but seems far from the "Ruby way":
def x(str, arr, rep='?')
i = 0
query.gsub(rep) { i+=1; arr[i-1] }
end
Is there a more idiomatic way of doing this? (Let me note that speed is the most important factor, of course.)
© Stack Overflow or respective owner