Project

General

Profile

Actions

Feature #6731

closed

add new method "Object.present?" as a counter to #empty?

Added by rogerdpack (Roger Pack) over 11 years ago. Updated over 11 years ago.

Status:
Rejected
Assignee:
-
Target version:
-
[ruby-core:46420]

Description

Basically Object#present? , "An object is present if it’s not #empty?" [2]
or, in other words, "is there any data inside?"

Examples:

[].present?
=> false
[3].present?
=> true
''.present?
=> false # because it's #empty?
'a'.present?
=> true
nil.present?
=> false

Example usage:

button.text=text if text.present? # I only care whether the text actually was set to something, and also don't want to worry about whether it's nil or not.

Thanks.

Basic implementation ([1]):

class Object

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

end

[1] http://stackoverflow.com/a/4649452/32453
[2] http://api.rubyonrails.org/classes/Object.html#method-i-present-3F they also use #blank but that's for a different feature request.


Files

present.txt (868 Bytes) present.txt rogerdpack (Roger Pack), 07/14/2012 03:39 PM

Updated by rogerdpack (Roger Pack) over 11 years ago

Adding "presentation" txt file.

Updated by claytrump (Clay Trump) over 11 years ago

Hum, wouldn't this conflict with Rails? In Rails, " ".present? is
falseclass Object

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

end

--

Updated by mame (Yusuke Endoh) over 11 years ago

  • Status changed from Open to Rejected

Roger Pack,

Sorry but this proposal was rejected at the developer meeting (7/21).

There are many possible definitions of "present?" depending upon
contexts. It's not Ruby's decision. People should decide the
definition for their context.

--
Yusuke Endoh

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0