For example, the words "stack", I want to get an array like:
['s', 'st', 'sta', ... 'stack', 't', 'ta', ... , 'c', 'ck', 'k']
I did this with the following code:
def split_word(str) result = [] chas = str.split("") len = chas.size (0..len-1).each do |i| (i..len-1).each do |j| result.push(chas[i..j].join) end end result.uniq end
Is there a better and cleaner way to do this? Thanks.
string ruby
Jimmy huang
source share