开发者

How to use present? in Ruby projects?

开发者 https://www.devze.com 2023-02-04 12:15 出处:网络
I wrote a script that uses Rails\' extensions to the Ruby core Object class, and now I want to spin it off on its own. 开发者_如何学CRather than rewriting these, is there any library or extension that

I wrote a script that uses Rails' extensions to the Ruby core Object class, and now I want to spin it off on its own. 开发者_如何学CRather than rewriting these, is there any library or extension that I can use to make them available independently?


You don't have to run Rails to use

require 'active_support/all'

or if you just want a specific extension, like blank? for string then just

require 'active_support/core_ext/string'

I looked up present? and it's actually defined as

def present?
  !blank?
end

blank defined in active support as

def blank?
  respond_to?(:empty?) ? !!empty? : !self
end


You can require specific functionality from ActiveSupport. In this case:

require 'active_support/core_ext/object/blank'
[].present? #=> false
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号