0

如果我在控制台上做这样的事情:

foo = "truthy"
bar = foo || "baz"

它按预期工作。bar如果它是真的,则分配双管道左侧变量的值,否则分配右侧变量的值。

在我的 Rails 应用程序中,我有这样的东西:

def fetch_feed_info
    info_loader = RSSInfoLoader.new(url)
    title = info_loader.title || ""
    website = info_loader.website || ""
end

但是,这在运行测试或在 Rails 控制台中创建类的实例时都会出现语法错误。错误是:

syntax error, unexpected tSTRING_BEG, expecting keyword_do or '{' or '('

谁能向我解释为什么?

堆栈跟踪

/Users/ricco/Sites/RoR/rss-plough/app/models/feed.rb:19: syntax error, unexpected tSTRING_BEG, expecting keyword_do or '{' or '('
website = info_loader.website || ""
                                   ^
from /Users/ricco/.rvm/gems/ruby-1.9.3-p327/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:251:in `block in require'
from /Users/ricco/.rvm/gems/ruby-1.9.3-p327/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:236:in `load_dependency'
from /Users/ricco/.rvm/gems/ruby-1.9.3-p327/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:251:in `require'
from /Users/ricco/.rvm/gems/ruby-1.9.3-p327/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:359:in `require_or_load'
from /Users/ricco/.rvm/gems/ruby-1.9.3-p327/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:313:in `depend_on'
from /Users/ricco/.rvm/gems/ruby-1.9.3-p327/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:225:in `require_dependency'
from /Users/ricco/.rvm/gems/ruby-1.9.3-p327/gems/railties-3.2.13/lib/rails/engine.rb:439:in `block (2 levels) in eager_load!'
from /Users/ricco/.rvm/gems/ruby-1.9.3-p327/gems/railties-3.2.13/lib/rails/engine.rb:438:in `each'
from /Users/ricco/.rvm/gems/ruby-1.9.3-p327/gems/railties-3.2.13/lib/rails/engine.rb:438:in `block in eager_load!'
from /Users/ricco/.rvm/gems/ruby-1.9.3-p327/gems/railties-3.2.13/lib/rails/engine.rb:436:in `each'
from /Users/ricco/.rvm/gems/ruby-1.9.3-p327/gems/railties-3.2.13/lib/rails/engine.rb:436:in `eager_load!'
from /Users/ricco/.rvm/gems/ruby-1.9.3-p327/gems/railties-3.2.13/lib/rails/application/finisher.rb:53:in `block in <module:Finisher>'
from /Users/ricco/.rvm/gems/ruby-1.9.3-p327/gems/railties-3.2.13/lib/rails/initializable.rb:30:in `instance_exec'
from /Users/ricco/.rvm/gems/ruby-1.9.3-p327/gems/railties-3.2.13/lib/rails/initializable.rb:30:in `run'
from /Users/ricco/.rvm/gems/ruby-1.9.3-p327/gems/railties-3.2.13/lib/rails/initializable.rb:55:in `block in run_initializers'
from /Users/ricco/.rvm/gems/ruby-1.9.3-p327/gems/railties-3.2.13/lib/rails/initializable.rb:54:in `each'
from /Users/ricco/.rvm/gems/ruby-1.9.3-p327/gems/railties-3.2.13/lib/rails/initializable.rb:54:in `run_initializers'
from /Users/ricco/.rvm/gems/ruby-1.9.3-p327/gems/railties-3.2.13/lib/rails/application.rb:136:in `initialize!'
from /Users/ricco/.rvm/gems/ruby-1.9.3-p327/gems/railties-3.2.13/lib/rails/railtie/configurable.rb:30:in `method_missing'
from /Users/ricco/Sites/RoR/rss-plough/config/environment.rb:5:in `<top (required)>'
from /Users/ricco/Sites/RoR/rss-plough/test/test_helper.rb:2:in `require'
from /Users/ricco/Sites/RoR/rss-plough/test/test_helper.rb:2:in `<top (required)>'
from test/unit/rssinfoloader_test.rb:1:in `require'
from test/unit/rssinfoloader_test.rb:1:in `<main>'

失败的功能:

def fetch_feed_info
    info_loader = RSSInfoLoader.new(url)
    title = info_loader.title || ""
    website = info_loader.website || ""
end
4

0 回答 0