新手问题 如何判断字符串中包含数字?

hgq114 · February 21, 2013 · Last by hgq114 replied at February 21, 2013 · 3077 hits

学习 Learn Ruby The Hard Way 的时候看到这样一段代码:

prompt; next_move = gets.chomp
if next_move.include? "0" or next_move.include? "1"
  how_much = next_move.to_i()
else
  dead("Man, learn to type a number.")
endm

next_move.include? "0" or next_move.include? "1" 这个式子怎么用更简单的方式来代替呢?要能直接判断字符串中包含数字

next_move =~ /0|1/

谢谢 1 楼,我找到个更普遍的表示:next_move =~ /^\d+(.\d+)?$/

You need to Sign in before reply, if you don't have an account, please Sign up first.