More Related Content
More from Masahiro Tomita (20)
Ruby24
- 2. Ruby 2.4 Powered by Rabbit 2.1.9
自己紹介
とみた まさひろ
http://tmtms.hatenablog.com
http://twitter.com/tmtms
https://github.com/tmtm
長野県北部在住プログラマー
Ruby / MySQL / Postfix
ときどきSoftwareDesignに寄稿
2016/12月号に掲載予定
- 3. Ruby 2.4 Powered by Rabbit 2.1.9
Ruby
オブジェクト指向スクリプト言語
整数、文字列、クラスなどほぼすべてがオブジェクト
毎年クリスマスにバージョンアップ
- 5. Ruby 2.4 Powered by Rabbit 2.1.9
新機能/変更点
よいまとめを発見
サンプルコードでわかる!Ruby 2.4の新機能と変更点
http://qiita.com/jnchito/items/9f9d45581816f121af07
- 6. Ruby 2.4 Powered by Rabbit 2.1.9
FixnumとBignumが廃止
# 2.3
123.class #=> Fixnum
12345678901234567890.class #=> Bignum
# 2.4
123.class #=> Integer
12345678901234567890.class #=> Integer
Fixnum #=> Integer
Bignum #=> Integer
- 7. Ruby 2.4 Powered by Rabbit 2.1.9
Enumerable#sum
全要素の合計
[1, 2, 3, 4].inject{|a,b| a+b} #=> 10
[1, 2, 3, 4].inject(:+) #=> 10
[1, 2, 3, 4].sum #=> 10
- 8. Ruby 2.4 Powered by Rabbit 2.1.9
Enumerable#sum
浮動小数点の誤差が蓄積しない
([0.1]*10).inject(:+) #=> 0.9999999999999999
([0.1]*10).sum #=> 1.0
- 9. Ruby 2.4 Powered by Rabbit 2.1.9
String#upcase, #downcase, #swapcase, #caitalize
非ASCIIラテン文字の大文字小文字化(全角も)
# 2.3
"ABCÅÔABC".downcase #=> "abcÅÔABC"
# 2.4
"ABCÅÔABC".downcase #=> "abcåôabc"
"ABCÅÔABC".downcase(:ascii) #=> "abcÅÔABC"
- 10. Ruby 2.4 Powered by Rabbit 2.1.9
Comparable#clamp
指定した範囲内に丸める
[-1, 0, 1, 2, 3, 4].map{|n| n.clamp(0, 3)}
#=> [0, 0, 1, 2, 3, 3]
- 11. Ruby 2.4 Powered by Rabbit 2.1.9
Numeric#ceil, #floor, #truncate
小数点以下の位置を指定可能
1.11111.ceil #=> 2
1.11111.ceil(1) #=> 1.2
1.11111.ceil(2) #=> 1.12
1.11111.ceil(3) #=> 1.112
1.11111.ceil(4) #=> 1.1112
- 12. Ruby 2.4 Powered by Rabbit 2.1.9
Integer#digits
各桁の数値を返す
1234.digits #=> [4, 3, 2, 1]
0x8A0.digits(16) #=> [0, 10, 8]
- 13. Ruby 2.4 Powered by Rabbit 2.1.9
Regexp#match?
比較だけ。$~, $`, $’ への設定なし
/bcd/ =~ "abcde" #=> 1
[$`, $~, $']
#=> ["a", #<MatchData "bcd">, "e"]
/bcd/.match?("abcde") #=> true
[nil, nil, nil]
- 14. Ruby 2.4 Powered by Rabbit 2.1.9
MatchData#named_captures, #values_at
m = "2016-10-22"
.match(/(?<year>dddd)-(?<month>dd)-(?<day>dd)/)
[m[:year], m[:month], m[:day]]
#=> ["2016", "10", "22"]
m.named_captures
#=> {"year"=>"2016", "month"=>"10", "day"=>"22"}
m.values_at(:year) #=> "2016"
- 15. Ruby 2.4 Powered by Rabbit 2.1.9
String.new(:capacity)
文字列の初期メモリ量を指定
String.new(capacity: 1000) #=> ""
- 16. Ruby 2.4 Powered by Rabbit 2.1.9
Enumerable#uniq
Array にはあったけど Enumerable には今までなかった
[1, 2, 3, 2, 1].uniq #=> [1, 2, 3]
{a:1, b:2, c:1}.uniq{|k,v| v})
#=> [[:a, 1], [:b, 2]]