コンソールで次のようなことをすると:
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