Skip to content

Instantly share code, notes, and snippets.

@marinados
Last active August 10, 2016 15:00
Show Gist options
  • Save marinados/31664c2b0e87989a08ce26029ad161ae to your computer and use it in GitHub Desktop.
Save marinados/31664c2b0e87989a08ce26029ad161ae to your computer and use it in GitHub Desktop.

.nil?

Ruby method. Works on any objects, including nil and NULL

.empty?

Ruby method Used on strings, array and hashes Doesn't work on nil! Returns true if the .length is 0 --> even if there's a whitespace in a string, it won't be empty

nil.empty? # NoMethodError
[nil].empty? # false
' '.empty? # false
```

## .blank?
ActiveRecord method
Can be used on any objects. 
For strings, arrays and hashes acts like .empty?
Except for whitespaces - they are considered blank
````ruby
nil.blank? # true
[nil].blank? # false
' '.blank? # true
```

##.present?
ActiveSupport method
it's the opposite of `.blank?``
`!something.blank? == something.present?``

````ruby
nil.present? # false
[nil].present? # true
' '.present? # false
```
##.any?
ActiveSupport method
Only works on arrays and AR collections
Passes every element of the collection to a given block and returns true if any of the element calls returns anything other that nil or false

````ruby
[nil, false].any? # false
[nil, false].any? { |element| element == true } # false
```

So, any vs present
```ruby
[nil, false].present? # true
[nil, false].any? # false
```




Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment