Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

あまり知られていないRubyの便利機能

73 views

Published on

[鹿児島Ruby会議01](https://k-ruby.github.io/kagoshima-rubykaigi01/)での発表資料です。

Published in: Technology
  • Be the first to comment

  • Be the first to like this

あまり知られていないRubyの便利機能

  1. 1. あまり知られていないRuby の便利機能 Kazuhiro NISHIYAMA 鹿児島Ruby会議01 2019/11/30 株式会社Ruby開発 Powered by Rabbit 2.2.1
  2. 2. 自己紹介 西山 和広 Ruby のコミッター twitter, github など: @znz 株式会社Ruby開発 www.ruby-dev.jp 1/14
  3. 3. String#undump String#dump ⇄ String#undump since ruby 2.5 String#dump ≠ String#inspect String#undump ≠ eval 2/14
  4. 4. Hash#transform_* convert from Hash to Hash Hash#transform_values{|v|...} since ruby 2.4 Hash#transform_keys{|k|...} since ruby 2.5 3/14
  5. 5. Hash#to_h with block Hash#to_h{|k,v|...} with block since ruby 2.6 without block since ruby 2.0.0 4/14
  6. 6. warn with uplevel: old: warn "#{caller(1, 1)[0]}: warning: message" new: warn "message", uplevel: 1 since ruby 2.5 5/14
  7. 7. abort(message) abort("failed message") ≒ warn("failed message"); exit(false) 6/14
  8. 8. rand(range) rand(range) rand(1..6) since ruby 1.9.3 NG: rand(endless_range) rand(1..) Errno::EDOM (Numerical argument out of domain) 7/14
  9. 9. String.new String.new.encoding → ASCII-8BIT String.new(encoding: 'euc- jp').encoding → EUC-JP ''.dup → UTF-8 (script encoding) +'' → UTF-8 (script encoding) ''.+@ (for method chain) (useful with frozen string literal) 8/14
  10. 10. String#gsub(pattern, hash) string.gsub(/['&"<>]/, { "'" => ''', '&' => '&amp;', '"' => '&quot;', '<' => '&lt;', '>' => '&gt;', }) 9/14
  11. 11. Regexp.union Regexp.union #=> /(?!)/ Regexp.union("penzance") #=> /penzance/ Regexp.union("a+b*c") #=> /a+b*c/ Regexp.union("skiing", "sledding") Regexp.union(["skiing", "sledding"]) #=> /skiing|sledding/ Regexp.union(/dogs/, /cats/i) #=> /(?-mix:dogs)|(?i-mx:cats)/ 10/14
  12. 12. String#*_with? "hello".start_with?("hell") #=> true "hello".start_with?(/H/i) #=> true # returns true if one of the prefixes matches. "hello".start_with?("heaven", "hell") #=> true "hello".start_with?("heaven", "paradise") #=> false "hello".end_with?("ello") #=> true # returns true if one of the +suffixes+ matches. "hello".end_with?("heaven", "ello") #=> true "hello".end_with?("heaven", "paradise") #=> false NG: starts_with?, ends_with? 11/14
  13. 13. String#{prepend,delet e_prefix,delete_suffix,c homp,chop} "end".prepend("prep") #=> "prepend" "prefix".delete_prefix("pre") #=> "fix" "suffix".delete_suffix("fix") #=> "suf" "suffix".chomp("fix") #=> "fix" "hellorn".chomp #=> "hello" "hellorn".chop #=> "hello" 12/14
  14. 14. String#{delete,tr} "hello".delete "l","lo" #=> "heo" "hello".delete "lo" #=> "he" "hello".delete "aeiou", "^e" #=> "hell" "hello".delete "ej-m" #=> "ho" "hello".tr('el', 'ip') #=> "hippo" "hello".tr('a-y', 'b-z') #=> "ifmmp" "hello".tr('^aeiou', '*') #=> "*e**o" 13/14
  15. 15. 参考文献 リファレンスマニュアル https://docs.ruby-lang.org/ja/ 間違いなどを見つけたら https://github.com/rurema/doctree もっと気軽に確認したいなら https://ruby-jp.github.io/ ruby-jp Slack の #rurema など 14/14Powered by Rabbit 2.2.1

×