3

My "Project" Table have invoice as integer attribute, Here I put nil object to this attribute in DB. During evaluation nil.empty? occurs. Code written at HAML extentions

- @project.each do |proj|
  =proj.invoice if !proj.invoice.blank? || !proj.invoice.empty? || !proj.invoice.nil?
  - @project_invoice=proj.invoice

  =@project_invoice=0 if proj.invoice.blank? || proj.invoice.empty? || proj.invoice.nil

I receive this error while running code.

NoMethodError: You have a nil object when you didn't expect it! You might have expected an instance of Array. The error occurred while evaluating nil.empty?

4 Answers 4

4

There's a few standard tests provided by Ruby and rails that can help, but you usually don't need to use all of them at once:

# Rails provided Object#blank? method
nil.blank? # => true
false.blank? # => true
''.blank? # => true
[ ].blank? # => true

# Ruby provided Object#nil? method
nil.nil? # => true
false.nil? # => false
''.nil? # => false
[ ].nil # => false

# Ruby class-specific #empty? method
nil.empty? # => error
false.empty? # => error
''.empty? # => true
[ ].empty? # => true

In your case the test you're probably looking for is actually a different one altogether. The opposite of blank? is present? and it comes in very handy for situations like this. You can even collapse down both of your inverted logical tests into a simple ternary query:

- @project_invoice = proj.present? ? proj.invoice : 0

More verbosely it looks like this:

- if (proj.present?)
  @project_invoice = proj.invoice
- else
  @project_invoice = 0

The present method verifies that the variable represents a non-nil, non-blank value of some sort.

Sign up to request clarification or add additional context in comments.

3 Comments

Really Good One, this single statement reduces my line of code. Thanx
@tadman..so empty? is defined for strings, arrays and hashes only..is that correct?
empty? is defined for a lot of things, String, Array and Hash as good examples, but there are others as it's a common method name to define. nil does not have an equivalent method.
0

The second condition has a misspelled variable name. It should be proj, not projt.

That would cause your issue.

1 Comment

No, I checked that one same issue arrises
0

if the invoice column is nill, then !proj.invoice.blank? evaluates to false, and the next test is done, !projt.invoice.empty?

since invoice is nil, you have nil.empty? which is an error, as empty? can not run on nil.

ruby-1.9.2-p0 > !nil.blank? || !nil.empty?
NoMethodError: You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.empty?

I think you are doing an overkill, since an integer should not be an array. I think you should just shorten it to only test blank?, as that catches an empty array too.

1 Comment

perhaps you meant !(proj.invoice.blank? || proj.invoice.empty? || proj.invoice.nil?) which is logically the same as !proj.invoice.blank? && !proj.invoice.empty? && !proj.invoice.nil? ... but unnecessary as blank? takes care of all the cases.
0

Is this is the code you are using. If this is the same code, I see a spelling mistake in the second line.

Can you try with

=proj.invoice if !proj.invoice.blank? || !proj.invoice.empty? || !proj.invoice.nil?

1 Comment

if proj.invoice is nil, you cannot check .empty? on it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.