かなり見落としてる。core_ext ら辺からいくつか。
Range#overlaps?
範囲が重なっているか。
(001): >> (1..3).overlaps?(0..1) => true (002): >> (1..3).overlaps?(1..3) => true (003): >> (1..3).overlaps?(3..4) => true (004): >> (1..3).overlaps?(4..5) => false
String#exclude?, Enumerable#exclude?
include? の逆。
(001): >> [1, 2, 3].exclude?(4) => true (002): >> [1, 2, 3].exclude?(3) => false
String#squish, String#squish!
ホワイトスペースを圧縮するやつ。
(001): >> %{ Multi-line string }.squish => "Multi-line string" (002): >> " foo bar \n \t boo".squish => "foo bar boo"
String#inquiry
ActiveSupport::StringInquirer のインスタンスを作る。
Rails.env.production? のやつ。
(001): >> env = 'production'.inquiry => "production" (002): >> env.production? => true (003): >> env.development? => false
String#dasherize
アンダースコアをダッシュにする。
(001): >> 'active_support'.dasherize => "active-support"
String#deconstantize
ネームスペースだけ取り出す。
(001): >> 'Net::HTTP'.deconstantize => "Net" (002): >> 'A::B::C::D'.deconstantize => "A::B::C"
String#demodulize
ネームスペースを取り去る。
(001): >> 'Net::HTTP'.demodulize => "HTTP" (002): >> 'A::B::C::D'.demodulize => "D"
Proc#bind
Proc を特定のコンテキストで評価できるやつ。
(001): >> ->{ puts self }.bind('HOGE') => #<Method: "HOGE".__bind_1362646558_946703> (002): >> ->{ puts self }.bind('HOGE').call HOGE => nil
Object#in?
include? のレシーバと引数が逆な感じ。
(001): >> 1.in?(1, 2, 3) => true (002): >> 1.in?(2, 3) => false
Enumerable#many?
2個以上。
(001): >> [].many? => false (002): >> [1].many? => false (003): >> [1, 2].many? => true
Enumerable#index_by
Hash を作る。
(001): >> person = Struct.new(:name) => #<Class:0x007fd444e8b0f0> (002): >> [person.new(name: 'a'), person.new(name: 'b')].index_by(&:name) => {{:name=>"a"}=>#<struct name={:name=>"a"}>, {:name=>"b"}=>#<struct name={:name=>"b"}>}
Module#anonymous?
名前がないと真。
(001): >> person.anonymous? => true (002): >> String.anonymous? => false
Array#in_groups
指定した数のグループに分ける。
(001): >> [1, 2, 3, 4, 5, 6].in_groups(2) => [[1, 2, 3], [4, 5, 6]] (002): >> [1, 2, 3, 4, 5, 6, 7].in_groups(2, '-') => [[1, 2, 3, 4], [5, 6, 7, "-"]]
Array#in_groups_of
指定した数の要素を持つグループを作る。
(001): >> [1, 2, 3, 4, 5, 6].in_groups_of(2) => [[1, 2], [3, 4], [5, 6]] (002): >> [1, 2, 3, 4, 5, 6, 7].in_groups_of(2, '-') => [[1, 2], [3, 4], [5, 6], [7, "-"]]
Array#uniq_by
任意のユニーク基準で重複除去。
(001): >> ['taro', 'jiro', 'TARO', 'JIRO'].uniq => ["taro", "jiro", "TARO", "JIRO"] (002): >> ['taro', 'jiro', 'TARO', 'JIRO'].uniq_by{|x| x.downcase } => ["taro", "jiro"]
File#atomic_write
一時ファイルを使ってアトミックにファイルを作成するやつ。
(001): >> File.exists?('hoge.txt') => false (002): >> open('hoge.txt', 'w') do |io| io.write('hogehoge') io.flush p File.exists?('hoge.txt') end true => true (007): >> File.exists?('hoge.txt') => true
(001): >> File.exists?('hoge.txt') => false (002): >> File.atomic_write('hoge.txt') do |io| io.write('hogehoge') io.flush p File.exists?('hoge.txt') end false => 1 (007): >> File.exists?('hoge.txt') => true