s = :split_this_world_game
def brk(start,s)
ar = []
cp = (s =~ /[_]/)
ss = s[start + 1,cp] if cp != nil
return s if cp == nil
ar.push(ss)
return ar.push(brk(cp,s[cp+1,s.size - ss.size])).flatten
end
p brk(-1,s)
The above code output:
["split", "orld", "_game", "game"]
But I wanted it as : ["split","this","world","game"]
Can anyone help me to find out where I did the wrong?
s = :split_this_game_world
def brk(s)
ar = []
cp = (s =~ /[_]/)
ss = s[0...cp] if cp != nil
return s if cp == nil
ar.push(ss)
return ar.push(brk(s[cp+1,s.size - ss.size])).flatten
end
p brk(s)
#=>["split", "this", "game", "world"]
Can this code be shorten or more Rubystic(obviously not converting the
symbol to String in first place)?
s = :split_this_game_world
def brk(s)
cp = (s =~ /[_]/)
ss = s[0...cp] if cp != nil
return s.to_sym if cp == nil
return [].push(ss.to_sym,brk(s[cp+1,s.size - ss.size])).flatten
end
p brk(s)
On 2013-05-08, at 8:24 AM, Love U Ruby <lists@ruby-forum.com> wrote:
Yes! I made it till now:
s = :split_this_game_world
def brk(s)
cp = (s =~ /[_]/)
ss = s[0...cp] if cp != nil
return s.to_sym if cp == nil
return .push(ss.to_sym,brk(s[cp+1,s.size - ss.size])).flatten
end
p brk(s)
More rubyish and even shorter (probably as short as possible)
but not recursive:
'this_is_a_test'.split('_')
···
Am 08.05.2013 14:24, schrieb Love U Ruby:
Yes! I made it till now:
s = :split_this_game_world
def brk(s)
cp = (s =~ /[_]/)
ss = s[0...cp] if cp != nil
return s.to_sym if cp == nil
return .push(ss.to_sym,brk(s[cp+1,s.size - ss.size])).flatten
end
p brk(s)