Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nil associations/embeds/links evaluate as true in some contexts #280

Closed
IdahoEv opened this issue Mar 13, 2012 · 3 comments
Closed

Nil associations/embeds/links evaluate as true in some contexts #280

IdahoEv opened this issue Mar 13, 2012 · 3 comments

Comments

@IdahoEv
Copy link
Contributor

IdahoEv commented Mar 13, 2012

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 >

@IdahoEv
Copy link
Contributor Author

IdahoEv commented Mar 13, 2012

version tested was current head: f28a4b9

@IdahoEv
Copy link
Contributor Author

IdahoEv commented Mar 13, 2012

I've bolded the places where the output is unexpected, in the console log.

@myronmarston
Copy link
Contributor

This is a duplicate of #197.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants