SlideShare a Scribd company logo
あまり知られていない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 II
Mitch 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 Java
Charles Anderson
 
Ruby's GC 20
Ruby's GC 20Ruby's GC 20
Ruby's GC 20
Narihiro Nakamura
 
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 programming
Iskren 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

Ubuntuのriscv64版をqemuで動かした at LILO&東海道らぐオフラインミーティング 2024-04-27
Ubuntuのriscv64版をqemuで動かした at LILO&東海道らぐオフラインミーティング 2024-04-27Ubuntuのriscv64版をqemuで動かした at LILO&東海道らぐオフラインミーティング 2024-04-27
Ubuntuのriscv64版をqemuで動かした at LILO&東海道らぐオフラインミーティング 2024-04-27
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.pdf
Kazuhiro Nishiyama
 
rubykaigi2022-rurema-history-and-future.pdf
rubykaigi2022-rurema-history-and-future.pdfrubykaigi2022-rurema-history-and-future.pdf
rubykaigi2022-rurema-history-and-future.pdf
Kazuhiro Nishiyama
 
qemuのriscv64にDebianを入れてみた
qemuのriscv64にDebianを入れてみたqemuのriscv64にDebianを入れてみた
qemuのriscv64にDebianを入れてみた
Kazuhiro Nishiyama
 
systemd 再入門
systemd 再入門systemd 再入門
systemd 再入門
Kazuhiro Nishiyama
 
Ruby 3.0.0 コネタ集
Ruby 3.0.0 コネタ集Ruby 3.0.0 コネタ集
Ruby 3.0.0 コネタ集
Kazuhiro Nishiyama
 
livedoor天気API終了対応
livedoor天気API終了対応livedoor天気API終了対応
livedoor天気API終了対応
Kazuhiro Nishiyama
 
Wireguard 実践入門
Wireguard 実践入門Wireguard 実践入門
Wireguard 実践入門
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
 
Dokku の紹介
Dokku の紹介Dokku の紹介
Dokku の紹介
Kazuhiro Nishiyama
 
Action Cableで簡易チャットを作ってみた
Action Cableで簡易チャットを作ってみたAction Cableで簡易チャットを作ってみた
Action Cableで簡易チャットを作ってみた
Kazuhiro Nishiyama
 
Ruby svn to git
Ruby svn to gitRuby svn to git
Ruby svn to git
Kazuhiro Nishiyama
 
Ruby 2.6 Update
Ruby 2.6 UpdateRuby 2.6 Update
Ruby 2.6 Update
Kazuhiro Nishiyama
 
最近のrubyのインストール方法
最近のrubyのインストール方法最近のrubyのインストール方法
最近のrubyのインストール方法
Kazuhiro Nishiyama
 
Language update 2018 - ruby
Language update 2018 - rubyLanguage update 2018 - ruby
Language update 2018 - ruby
Kazuhiro Nishiyama
 

More from Kazuhiro Nishiyama (20)

Ubuntuのriscv64版をqemuで動かした at LILO&東海道らぐオフラインミーティング 2024-04-27
Ubuntuのriscv64版をqemuで動かした at LILO&東海道らぐオフラインミーティング 2024-04-27Ubuntuのriscv64版をqemuで動かした at LILO&東海道らぐオフラインミーティング 2024-04-27
Ubuntuのriscv64版をqemuで動かした at LILO&東海道らぐオフラインミーティング 2024-04-27
 
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
 

Recently uploaded

State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
Prayukth K V
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
Laura Byrne
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
RTTS
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
Product School
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
Cheryl Hung
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
DianaGray10
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
UiPathCommunity
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
DianaGray10
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
Jemma Hussein Allen
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
Safe Software
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Tobias Schneck
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Product School
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
Product School
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
Elena Simperl
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
91mobiles
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
Kari Kakkonen
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
Elena Simperl
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
Alan Dix
 

Recently uploaded (20)

State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
 

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