SlideShare a Scribd company logo
1 of 15
Download to read offline
あまり知られていないRuby
の便利機能
Kazuhiro NISHIYAMA
鹿児島Ruby会議01
2019/11/30
株式会社Ruby開発
Powered by Rabbit 2.2.1
自己紹介
西山 和広
Ruby のコミッター
twitter, github など: @znz
株式会社Ruby開発 www.ruby-dev.jp
1/14
String#undump
String#dump ⇄ String#undump
since ruby 2.5
String#dump ≠ String#inspect
String#undump ≠ eval
2/14
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
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
warn with uplevel:
old: warn "#{caller(1, 1)[0]}:
warning: message"
new: warn "message", uplevel: 1
since ruby 2.5
5/14
abort(message)
abort("failed message")
≒ warn("failed message");
exit(false)
6/14
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
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
String#gsub(pattern,
hash)
string.gsub(/['&"<>]/, {
"'" => '&#39;',
'&' => '&amp;',
'"' => '&quot;',
'<' => '&lt;',
'>' => '&gt;',
})
9/14
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
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
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
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
参考文献
リファレンスマニュアル
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

More Related Content

What's hot

MongoDB, Node.js And You: PART II
MongoDB, Node.js And You: PART IIMongoDB, Node.js And You: PART II
MongoDB, Node.js And You: PART IIMitch Pirtle
 
Node.js for Rubists
Node.js for RubistsNode.js for Rubists
Node.js for RubistsSagiv Ofek
 
Groovy a Scripting Language for Java
Groovy a Scripting Language for JavaGroovy a Scripting Language for Java
Groovy a Scripting Language for JavaCharles Anderson
 
Introduction to Python
Introduction to PythonIntroduction to Python
Introduction to PythonSway Wang
 
The art of concurrent programming
The art of concurrent programmingThe art of concurrent programming
The art of concurrent programmingIskren Chernev
 

What's hot (7)

MongoDB, Node.js And You: PART II
MongoDB, Node.js And You: PART IIMongoDB, Node.js And You: PART II
MongoDB, Node.js And You: PART II
 
Node.js for Rubists
Node.js for RubistsNode.js for Rubists
Node.js for Rubists
 
Groovy a Scripting Language for Java
Groovy a Scripting Language for JavaGroovy a Scripting Language for Java
Groovy a Scripting Language for Java
 
20140918 ruby kaigi2014
20140918 ruby kaigi201420140918 ruby kaigi2014
20140918 ruby kaigi2014
 
Ruby's GC 20
Ruby's GC 20Ruby's GC 20
Ruby's GC 20
 
Introduction to Python
Introduction to PythonIntroduction to Python
Introduction to Python
 
The art of concurrent programming
The art of concurrent programmingThe art of concurrent programming
The art of concurrent programming
 

More from Kazuhiro Nishiyama

lilo.linux.or.jp を buster から bullseye に上げた
lilo.linux.or.jp を buster から bullseye に上げたlilo.linux.or.jp を buster から bullseye に上げた
lilo.linux.or.jp を buster から bullseye に上げたKazuhiro Nishiyama
 
小規模個人アプリをRails 7.xにバージョンアップした話
小規模個人アプリをRails 7.xにバージョンアップした話小規模個人アプリをRails 7.xにバージョンアップした話
小規模個人アプリをRails 7.xにバージョンアップした話Kazuhiro Nishiyama
 
Ruby リファレンスマニュアル改善計画 2022 進捗報告
Ruby リファレンスマニュアル改善計画 2022 進捗報告Ruby リファレンスマニュアル改善計画 2022 進捗報告
Ruby リファレンスマニュアル改善計画 2022 進捗報告Kazuhiro Nishiyama
 
fukuoka03-rubima-reboot-rubyist-magazine-reboot.pdf
fukuoka03-rubima-reboot-rubyist-magazine-reboot.pdffukuoka03-rubima-reboot-rubyist-magazine-reboot.pdf
fukuoka03-rubima-reboot-rubyist-magazine-reboot.pdfKazuhiro Nishiyama
 
rubykaigi2022-rurema-history-and-future.pdf
rubykaigi2022-rurema-history-and-future.pdfrubykaigi2022-rurema-history-and-future.pdf
rubykaigi2022-rurema-history-and-future.pdfKazuhiro Nishiyama
 
qemuのriscv64にDebianを入れてみた
qemuのriscv64にDebianを入れてみたqemuのriscv64にDebianを入れてみた
qemuのriscv64にDebianを入れてみたKazuhiro Nishiyama
 
workflow,job,step の使い分けの基準を考える
workflow,job,step の使い分けの基準を考えるworkflow,job,step の使い分けの基準を考える
workflow,job,step の使い分けの基準を考えるKazuhiro Nishiyama
 
Dockerのオフィシャルrubyイメージとは?
Dockerのオフィシャルrubyイメージとは?Dockerのオフィシャルrubyイメージとは?
Dockerのオフィシャルrubyイメージとは?Kazuhiro Nishiyama
 
チャットボットのススメ
チャットボットのススメチャットボットのススメ
チャットボットのススメKazuhiro Nishiyama
 
Action Cableで簡易チャットを作ってみた
Action Cableで簡易チャットを作ってみたAction Cableで簡易チャットを作ってみた
Action Cableで簡易チャットを作ってみたKazuhiro Nishiyama
 
最近のrubyのインストール方法
最近のrubyのインストール方法最近のrubyのインストール方法
最近のrubyのインストール方法Kazuhiro Nishiyama
 
systemdでよく使うサブコマンド
systemdでよく使うサブコマンドsystemdでよく使うサブコマンド
systemdでよく使うサブコマンドKazuhiro Nishiyama
 

More from Kazuhiro Nishiyama (20)

lilo.linux.or.jp を buster から bullseye に上げた
lilo.linux.or.jp を buster から bullseye に上げたlilo.linux.or.jp を buster から bullseye に上げた
lilo.linux.or.jp を buster から bullseye に上げた
 
小規模個人アプリをRails 7.xにバージョンアップした話
小規模個人アプリをRails 7.xにバージョンアップした話小規模個人アプリをRails 7.xにバージョンアップした話
小規模個人アプリをRails 7.xにバージョンアップした話
 
Ruby リファレンスマニュアル改善計画 2022 進捗報告
Ruby リファレンスマニュアル改善計画 2022 進捗報告Ruby リファレンスマニュアル改善計画 2022 進捗報告
Ruby リファレンスマニュアル改善計画 2022 進捗報告
 
fukuoka03-rubima-reboot-rubyist-magazine-reboot.pdf
fukuoka03-rubima-reboot-rubyist-magazine-reboot.pdffukuoka03-rubima-reboot-rubyist-magazine-reboot.pdf
fukuoka03-rubima-reboot-rubyist-magazine-reboot.pdf
 
rubykaigi2022-rurema-history-and-future.pdf
rubykaigi2022-rurema-history-and-future.pdfrubykaigi2022-rurema-history-and-future.pdf
rubykaigi2022-rurema-history-and-future.pdf
 
qemuのriscv64にDebianを入れてみた
qemuのriscv64にDebianを入れてみたqemuのriscv64にDebianを入れてみた
qemuのriscv64にDebianを入れてみた
 
systemd 再入門
systemd 再入門systemd 再入門
systemd 再入門
 
Ruby 3.0.0 コネタ集
Ruby 3.0.0 コネタ集Ruby 3.0.0 コネタ集
Ruby 3.0.0 コネタ集
 
livedoor天気API終了対応
livedoor天気API終了対応livedoor天気API終了対応
livedoor天気API終了対応
 
Wireguard 実践入門
Wireguard 実践入門Wireguard 実践入門
Wireguard 実践入門
 
workflow,job,step の使い分けの基準を考える
workflow,job,step の使い分けの基準を考えるworkflow,job,step の使い分けの基準を考える
workflow,job,step の使い分けの基準を考える
 
Dockerのオフィシャルrubyイメージとは?
Dockerのオフィシャルrubyイメージとは?Dockerのオフィシャルrubyイメージとは?
Dockerのオフィシャルrubyイメージとは?
 
チャットボットのススメ
チャットボットのススメチャットボットのススメ
チャットボットのススメ
 
Dokku の紹介
Dokku の紹介Dokku の紹介
Dokku の紹介
 
Action Cableで簡易チャットを作ってみた
Action Cableで簡易チャットを作ってみたAction Cableで簡易チャットを作ってみた
Action Cableで簡易チャットを作ってみた
 
Ruby svn to git
Ruby svn to gitRuby svn to git
Ruby svn to git
 
Ruby 2.6 Update
Ruby 2.6 UpdateRuby 2.6 Update
Ruby 2.6 Update
 
最近のrubyのインストール方法
最近のrubyのインストール方法最近のrubyのインストール方法
最近のrubyのインストール方法
 
Language update 2018 - ruby
Language update 2018 - rubyLanguage update 2018 - ruby
Language update 2018 - ruby
 
systemdでよく使うサブコマンド
systemdでよく使うサブコマンドsystemdでよく使うサブコマンド
systemdでよく使うサブコマンド
 

Recently uploaded

Connecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfConnecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfNeo4j
 
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS:  6 Ways to Automate Your Data IntegrationBridging Between CAD & GIS:  6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integrationmarketing932765
 
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPathCommunity
 
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Mark Goldstein
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 
Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesKari Kakkonen
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsNathaniel Shimoni
 
QCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architecturesQCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architecturesBernd Ruecker
 
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical InfrastructureVarsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructureitnewsafrica
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch TuesdayIvanti
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentPim van der Noll
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Alkin Tezuysal
 
Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfIngrid Airi González
 
A Framework for Development in the AI Age
A Framework for Development in the AI AgeA Framework for Development in the AI Age
A Framework for Development in the AI AgeCprime
 
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...Wes McKinney
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024TopCSSGallery
 
Data governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationData governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationKnoldus Inc.
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Farhan Tariq
 

Recently uploaded (20)

Connecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfConnecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdf
 
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS:  6 Ways to Automate Your Data IntegrationBridging Between CAD & GIS:  6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integration
 
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to Hero
 
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 
Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examples
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directions
 
QCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architecturesQCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architectures
 
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical InfrastructureVarsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch Tuesday
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
 
Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdf
 
A Framework for Development in the AI Age
A Framework for Development in the AI AgeA Framework for Development in the AI Age
A Framework for Development in the AI Age
 
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024
 
Data governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationData governance with Unity Catalog Presentation
Data governance with Unity Catalog Presentation
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...
 

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