开发者

Ruby continue in case

For example, I have the following code.

str = 'delay loss duplicate etc'
case str
    when /delay/ then
      puts 'delay'
    when /loss/ then
      puts 'loss'
end

I want give.

delay
loss

Is it possible in ruby not break all next conditions 开发者_运维百科when the first coincidence?


I'm not sure case is really what you want here.

str = 'delay loss duplicate etc'
regex = { /delay/ => 'delay', /loss/ => 'loss' }
regex.each { |r, s| puts s if str =~ r } 

If you want another regex, just add a key/value pair to the hash.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜