Reputation: 924
I have a method like this in rails app:
def current_user
return @current_user if @current_user.present?
@current_user = current_user_session && current_user_session.record
end
I haven't used the .present?
method before so I went into my interactive ruby shell to play around with it.
Whenever I use xxx.present?
it returns a NoMethodError. It doesn't matter if xxx
is a string, number, array anything.
How can I use this method?
Upvotes: 14
Views: 17457
Reputation: 6871
present
is a method provided by ActiveSupport
. It is not a part of core ruby. That is why you are not able to use it in the ruby shell. To play around with it, you will need to require the relevant libraries in your console (e.g. irb / pry):
require 'active_support'
require 'active_support/core_ext'
This way, you will have access to all the utility methods provided by activesupport
.
Upvotes: 31
Reputation: 13054
As mentioned above, .present
is not in the ruby standard library. Instead, it's in active_support (docs.)
To use active_support, first install the gem, then require it in irb or your ruby script. More information on how to selectively load activesupport extensions can be found in the Rails guides.
Upvotes: 3