我正在学习一些 Rspec 的东西,并且不小心在我的模型类中引入了一些代码,我希望这会产生错误。但令我惊讶的是,没有。
class Address < ActiveRecord::Base
attr_accessible :city, :country, :person_id, :street, :zip
validates_presence_of :city, :zip, :street
before_save :setDefaultCountry
# -- Beginning strange code --
if true
puts "Hey! I shouldn't be able to do this"
end
# -- End of strange code --
private
def setDefaultCountry
if self.country.blank?
self.country = "US"
end
end
end
这是 rails 控制台输出:
Arman$ rails console
Loading development environment (Rails 3.2.3)
1.9.3p194 :001 > a = Address.new
Hey! I shouldn't be able to do this
=> #<Address id: nil, street: nil, city: nil, zip: nil, country: nil, person_id: nil, created_at: nil, updated_at: nil>
1.9.3p194 :002 >
为什么 ruby 不抱怨在类定义中添加了奇怪的代码?