开发者

Ruby continue in case

开发者 https://www.devze.com 2023-03-25 09:52 出处:网络
For example, I have the following code. str = \'delay loss duplicate etc\' case str when /delay/ then puts \'delay\'

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

精彩评论

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