私はいくつかの 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が文句を言わないのはなぜですか?