Rails has useful present?
method. How can I check the same in Hanami?
Asked
Active
Viewed 872 times
2

arturtr
- 1,115
- 9
- 18
-
What is the use case for using `present?`. Is that for parameter checking? There are better ways of doing that than calling `present?`. – Aleksander Pohl Jan 04 '18 at 13:44
1 Answers
4
present?
is the opposite of blank?
in Ruby on Rails.
You could use Hanami::Utils::Blank
:
require 'hanami/utils/blank'
Hanami::Utils::Blank.blank?(nil) #=> true
Hanami::Utils::Blank.blank?(' ') #=> true
Hanami::Utils::Blank.blank?('Artur') #=> false
However there are two concerns:
blank?
is not equal toHanami::Utils::Blank.blank?
Hanami::Utils::Blank.blank?
is considered to be part of a private API
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
You could use ActiveSupport
without Ruby on Rails
Active Support is a collection of utility classes and standard library extensions. It's a separate gem and you can use it independently.
You could extend Object
:
class Object
def blank?
respond_to?(:empty?) ? !!empty? : !self
end
def present?
!blank?
end
end
And the last option
You may prefer using pure Ruby and its nil?
and empty?
methods if semantics is suitable.

Danil Speransky
- 29,891
- 5
- 68
- 79
-
2And there are plenty of ways to do this cleanly in [pure Ruby](https://stackoverflow.com/a/26877095/3784008). – anothermh Sep 17 '17 at 22:38