From: 6ftdan@... Date: 2016-02-10T04:02:52+00:00 Subject: [ruby-core:73755] [Ruby trunk Feature#12059] `Array#single?`, `Hash#single?` Issue #12059 has been updated by Daniel P. Clark. Right. Given the original examples by the OP `Enumerable#one?` works. ~~~ruby a = ["object1", "object2"] "There #{a.one? ? "is" : "are"} #{a.length} #{a.one? ? "object" : "objects"}." # => "There are 2 objects." [1, 2, 2, 1].uniq.one? # => false [1, 1, 1, 1].uniq.one? # => true ~~~ ---------------------------------------- Feature #12059: `Array#single?`, `Hash#single?` https://bugs.ruby-lang.org/issues/12059#change-56941 * Author: Tsuyoshi Sawada * Status: Open * Priority: Normal * Assignee: ---------------------------------------- There are some use cases when one wants to check if an array or a hash has exactly one element. I propose `Array#single?` and `Hash#single?` that checks for such cases and returns either `true` or `false`. This is an analogy from the `empty?` method on the respective class. * When creating an inflectional form out of an array: ~~~ruby a = ["object1", "object2"] "There #{a.single ? "is" : "are"} #{a.length} #{a.single? ? "object" : "objects"}." # => "There are 2 objects." ~~~ * When checking if all elements of the array are the same: ~~~ruby [1, 2, 2, 1].uniq.single? # => false [1, 1, 1, 1].uniq.single? # => true ~~~ -- https://bugs.ruby-lang.org/ Unsubscribe: