$ irb cannot load such file -- wirble >> puts 'ok' ok => nil >> puts 'ok' if true ok => nil >> if true; puts 'ok' >> end ok => nil 若 if 條件與敘述同一行,之間要加;字符,且後面要有end。 若敘述行在前,if條件再後,不需加;字符,而且不需要有end。
也請參看 Ruby Style Guide: https://github.com/styleguide/ruby 用then雖然也行,卻不是ruby慣用的風格, 大概是習慣其他編程語言遺留的習性, 還是盡可能避免在 if 裡用 then, Never use then for multi-line if/unless. # bad if some_condition then # body omitted end # good if some_condition # body omitted end 另外也提到: Favor modifier if/unless usage when you have a single-line body. # bad if some_condition do_something end # good do_something if some_condition 盡量在 ruby 裡入境隨俗吧! 避免在之前習慣別的編程語法帶到 ruby 裡。