Ruby 1.9 % 操作符对于nil会抛异常

Ruby 1.9 测试:

# irb
irb(main):001:0> "%d" % nil
TypeError: can't convert nil into Integer
        from (irb):1:in `%'
        from (irb):1
        from /bin/irb:12:in `<main>'

Ruby 1.8 不会抛出异常,默认会转换成0:

irb
irb(main):001:0> "%d" % nil
=> "0"

你可能感兴趣的:(Ruby)