本文簡要介紹ruby語言中 Set.classify
的用法。
用法
classify() { |o| ... }
根據給定塊的返回值對集合進行分類,並返回 {value => set of elements} 對的哈希。該塊對集合中的每個元素調用一次,並將元素作為參數傳遞。
require 'set'
files = Set.new(Dir.glob("*.rb"))
hash = files.classify { |f| File.mtime(f).year }
hash #=> {2000=>#<Set: {"a.rb", "b.rb"}>,
# 2001=>#<Set: {"c.rb", "d.rb", "e.rb"}>,
# 2002=>#<Set: {"f.rb"}>}
如果沒有給出塊,則返回一個枚舉器。
相關用法
- Ruby Set.clear用法及代碼示例
- Ruby Set.replace用法及代碼示例
- Ruby Set.==用法及代碼示例
- Ruby Set.disjoint?用法及代碼示例
- Ruby Set.[]用法及代碼示例
- Ruby Set.===用法及代碼示例
- Ruby Set.-用法及代碼示例
- Ruby Set.to_a用法及代碼示例
- Ruby Set.add?用法及代碼示例
- Ruby Set.add用法及代碼示例
- Ruby Set.|用法及代碼示例
- Ruby Set.^用法及代碼示例
- Ruby Set.&用法及代碼示例
- Ruby Set.new用法及代碼示例
- Ruby Set.divide用法及代碼示例
- Ruby Set.intersect?用法及代碼示例
- Ruby Set flatten()用法及代碼示例
- Ruby Set intersection()用法及代碼示例
- Ruby Set類用法及代碼示例
- Ruby Set size()用法及代碼示例
- Ruby Set compare_by_identity?用法及代碼示例
- Ruby Set merge()用法及代碼示例
- Ruby Set add?用法及代碼示例
- Ruby Set compare_by_identity()用法及代碼示例
- Ruby Set flatten!()用法及代碼示例
注:本文由純淨天空篩選整理自ruby-lang.org大神的英文原創作品 Set.classify。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。