You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If i embed or link another object, and the associated object is nil, the association method evaluates as 'true'. This means you have to explictly call .nil?() on an association to see if the object exists or not.
Since this is not at all how Ruby programmers expect nil to behave, it can result in some extremely confusing debugging situations ... if .nil? works, but unless does not. ||= does not work, etc.
class MyModel
include Ripple::Document
one :foo, :class_name => "EmbeddedModel"
property :name, String
end
class EmbeddedModel
include Ripple::EmbeddedDocument
If i embed or link another object, and the associated object is nil, the association method evaluates as 'true'. This means you have to explictly call .nil?() on an association to see if the object exists or not.
Since this is not at all how Ruby programmers expect nil to behave, it can result in some extremely confusing debugging situations ... if .nil? works, but unless does not. ||= does not work, etc.
class MyModel
include Ripple::Document
one :foo, :class_name => "EmbeddedModel"
property :name, String
end
class EmbeddedModel
include Ripple::EmbeddedDocument
property :awesomeness, Integer
end
$ rails console
Loading development environment (Rails 3.1.3)
ruby-1.9.2-p290 :001 > mm = MyModel.new
=> <MyModel:[new] name=nil>
ruby-1.9.2-p290 :002 > mm.foo
=> nil
ruby-1.9.2-p290 :003 > print "no foo" if mm.foo.nil?
no foo => nil
ruby-1.9.2-p290 :004 > print "no foo" unless mm.foo
=> nil
ruby-1.9.2-p290 :005 > mm.foo ||= EmbeddedModel.new(:awesomeness => 100)
=> nil
ruby-1.9.2-p290 :006 > mm.foo
=> nil
ruby-1.9.2-p290 :007 > mm.foo = EmbeddedModel.new(:awesomeness => 100)
=>
ruby-1.9.2-p290 :008 > mm.foo
=>
ruby-1.9.2-p290 :009 > mm = MyModel.new
=> <MyModel:[new] name=nil>
ruby-1.9.2-p290 :010 > mm.foo.class
=> NilClass
ruby-1.9.2-p290 :011 > mm.foo == nil
=> true
ruby-1.9.2-p290 :012 > print "nil" unless nil
nil => nil
ruby-1.9.2-p290 :013 > print "it's nil" unless nil
it's nil => nil
ruby-1.9.2-p290 :014 > print "it's nil" unless mm.foo
=> nil
ruby-1.9.2-p290 :015 > mm.foo.nil?
=> true
ruby-1.9.2-p290 :016 >
The text was updated successfully, but these errors were encountered: