data.select { |h| h[:sex] == sex}.all? { |a| a[:age] > age_is_greater_than }
先select出來 再用all來找出是否全部符合條件
all?() public
Passes each element of the collection to the given block. The method returns true if the block never returns false or nil. If the block is not given, Ruby adds an implicit block of {|obj| obj} (that is all? will return true only if none of the collection members are false or nil.)
bsearch
ary = [0, 4, 7, 10, 12]
# try to find v such that 4 <= v < 8
ary.bsearch {|x| 1 - x / 4 } #=> 4 or 7
# try to find v such that 8 <= v < 10
ary.bsearch {|x| 4 - x / 2 } #=> nil
我簡單的理解成當要用binary search來找array時 如何設計條件
要找出4 <= v < 8的 就是條件中{|x| 1 - x / 4 } x的範圍為多少時 結果為0
In find-any mode (this behaves like libc’s bsearch(3)), the block must return a number, and there must be two indices i and j (0 <= i <= j <= ary.size) so that:
the block returns a positive number for ary if 0 <= k < i,
the block returns zero for ary if i <= k < j, and
the block returns a negative number for ary if j <= k < ary.size.
Under this condition, this method returns any element whose index is within i…j. If i is equal to j (i.e., there is no element that satisfies the block), this method returns nil.
data.select { |h| h[:sex] == sex}.all? { |a| a[:age] > age_is_greater_than }
all?() public
Passes each element of the collection to the given block. The method returns true if the block never returns false or nil. If the block is not given, Ruby adds an implicit block of {|obj| obj} (that is all? will return true only if none of the collection members are false or nil.)
ary = [0, 4, 7, 10, 12]
# try to find v such that 4 <= v < 8
ary.bsearch {|x| 1 - x / 4 } #=> 4 or 7
# try to find v such that 8 <= v < 10
ary.bsearch {|x| 4 - x / 2 } #=> nil
要找出4 <= v < 8的 就是條件中{|x| 1 - x / 4 } x的範圍為多少時 結果為0
沒有留言:
張貼留言