mant1988
mant1988

Reputation: 55

Rails: What the best way to check empty of integer?

My problem is:

"1".empty? == false

But:

1.empty? == NoMethodError: undefined method `empty?' for 1:Fixnum

If I change it to blank?

"1".blank? == false

1.blank? == false

So, I want to know the other way to check an integer is empty or not because I hate blank?

Upvotes: 2

Views: 4885

Answers (1)

Mini John
Mini John

Reputation: 7941

Here are some examples about nil? and empty?

nil.nil?
# => true

false.nil?
# => false

1.nil?
# => false

0.nil?
# => false

"".nil?
# => false

[].nil?
# => false

"".empty?
# => true

"abc".empty?
# => false

[].empty?
# => true

[1, 2, 3].empty?
=> false

1.empty?
=> NoMethodError

The last example means that the empty? method is not defined for class Fixnum

Upvotes: 4

Related Questions