我正在使用Rails 4.2.7并具有以下内容来修剪特定表达式中的所有空格...
puts "division: #{division}$$"
division = division.gsub(/\A\p{Space}+|\p{Space}+\z/, '')
当我运行它时,最终我的程序因此输出而死...
division: 18-29$$
Error during processing: undefined method `gsub' for nil:NilClass
/Users/mikeb/Documents/workspace/runtrax/app/models/race_time.rb:27:in `block in <class:RaceTime>'
/Users/mikeb/.rvm/gems/ruby-2.3.0/gems/activesupport-4.2.7.1/lib/active_support/callbacks.rb:448:in `instance_exec'
/Users/mikeb/.rvm/gems/ruby-2.3.0/gems/activesupport-4.2.7.1/lib/active_support/callbacks.rb:448:in `block in make_lambda'
/Users/mikeb/.rvm/gems/ruby-2.3.0/gems/activesupport-4.2.7.1/lib/active_support/callbacks.rb:228:in `block in halting_and_conditional'
/Users/mikeb/.rvm/gems/ruby-2.3.0/gems/activesupport-4.2.7.1/lib/active_support/callbacks.rb:506:in `block in call'
/Users/mikeb/.rvm/gems/ruby-2.3.0/gems/activesupport-4.2.7.1/lib/active_support/callbacks.rb:506:in `each'
/Users/mikeb/.rvm/gems/ruby-2.3.0/gems/activesupport-4.2.7.1/lib/active_support/callbacks.rb:506:in `call'
/Users/mikeb/.rvm/gems/ruby-2.3.0/gems/activesupport-4.2.7.1/lib/active_support/callbacks.rb:92:in `__run_callbacks__'
…
我不明白为什么gsub认为它在上面的行中得到一个nil,字符串显然不是空的(打印出18-29的值)。奇怪的是,我无法在Rails控制台中重现这一点,所以我想知道这是其他的事情,或者是否有更好的方法来编写上述内容。