i found example of using #any?
on hash bit tricky:
"with hash, can use these in 2 ways. either 1 argument 2 element array of key-value pair. candidate[0]
key , candidate[1]
value.
{:locke => 4, :hugo => 8}.any? { |candidate| candidate[1] > 4 }
this returns true because value of second candidate :hugo
greater 4
."
could point me someplace explains happened here? couldn't find relevant question on so. lot in advance.
if print candidate become easy understand:
{:locke => 4, :hugo => 8}.any? { |candidate| puts candidate.to_s } # [:locke, 4] # [:hugo, 8]
the any?
method treating each key-value pair of hash two-element array, means hash treated array of arrays.
the block passed any?
(i.e., { |candidate| candidate[1] > 4 }
) returns true
if of the second elements (i.e., 4 , 8) ever > 4
, false
otherwise. 8 > 4
, result true
.
from official docs, any?
method:
passes each element of collection given block. method returns true if block ever returns value other false or nil. if block not given, ruby adds implicit block of { |obj| obj } cause any? return true if @ least 1 of collection members not false or nil.
Comments
Post a Comment