Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
String#split何もわかっていなかった/didn-t-know-anything-ab...
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
Masatoshi Moritsuka
May 19, 2022
Programming
92
0
Share
String#split何もわかっていなかった/didn-t-know-anything-about-string-split
Masatoshi Moritsuka
May 19, 2022
More Decks by Masatoshi Moritsuka
See All by Masatoshi Moritsuka
Rails の CLI ツールの書き方/writing-rails-cli-tool
sanfrecce_osaka
0
41
Time.zone.parse('dark')/time-zone-parse-dark
sanfrecce_osaka
0
110
外部APIが絡むテストをちょっといい感じに書く/a-little-nice-writing-external-api-testing
sanfrecce_osaka
0
29
gem_rbs_collection へのコントリビュートから始める Ruby の型の世界/contributing-gem-rbs-collection
sanfrecce_osaka
0
570
Rails と人魚の話/rails-and-mermaid
sanfrecce_osaka
0
460
パターンマッチ使ってるかい?(kyobashi.rb)/use-ruby-s-pattern-matching-on-kyobashi-rb
sanfrecce_osaka
0
250
ApplicationController の継承を分割してエラーを減らした話/dividing-application-controller
sanfrecce_osaka
1
390
Input object ではじめる入力値検証/input-value-validation-using-input-object
sanfrecce_osaka
0
590
実例で学ぶRailsアプリケーションデバッグ入門 〜ログインできちゃってました編〜/rails-application-debug-introduction
sanfrecce_osaka
2
910
Other Decks in Programming
See All in Programming
Strategy for Finding a Problem for OSS: With Real Examples
kibitan
0
140
Rethinking API Platform Filters
vinceamstoutz
0
11k
今年もTECHSCOREブログを書き続けます!
hiraoku101
0
230
Xdebug と IDE による デバッグ実行の仕組みを見る / Exploring-How-Debugging-Works-with-Xdebug-and-an-IDE
shin1x1
0
340
ドメインイベントでビジネスロジックを解きほぐす #phpcon_odawara
kajitack
2
110
事業会社でのセキュリティ長期インターンについて
masachikaura
0
230
RSAが破られる前に知っておきたい 耐量子計算機暗号(PQC)入門 / Intro to PQC: Preparing for the Post-RSA Era
mackey0225
3
120
Symfony + NelmioApiDocBundle を使った スキーマ駆動開発 / Schema Driven Development with NelmioApiDocBundle
okashoi
0
270
Geminiをパートナーに神社DXシステムを個人開発した話(いなめぐDX 開発振り返り)
fujiba
0
140
実践ハーネスエンジニアリング #MOSHTech
kajitack
7
5.9k
forteeの改修から振り返るPHPerKaigi 2026
muno92
PRO
3
240
PHP でエミュレータを自作して Ubuntu を動かそう
m3m0r7
PRO
2
170
Featured
See All Featured
Crafting Experiences
bethany
1
110
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
254
22k
How to build an LLM SEO readiness audit: a practical framework
nmsamuel
1
710
For a Future-Friendly Web
brad_frost
183
10k
The State of eCommerce SEO: How to Win in Today's Products SERPs - #SEOweek
aleyda
2
10k
AI: The stuff that nobody shows you
jnunemaker
PRO
4
520
Building an army of robots
kneath
306
46k
Efficient Content Optimization with Google Search Console & Apps Script
katarinadahlin
PRO
1
480
HDC tutorial
michielstock
1
610
30 Presentation Tips
portentint
PRO
1
270
Optimising Largest Contentful Paint
csswizardry
37
3.6k
DBのスキルで生き残る技術 - AI時代におけるテーブル設計の勘所
soudai
PRO
64
53k
Transcript
String#split 何もわかって いなかった 森塚 真年(@sanfrecce_osaka) 2022/05/18 K-Ruby#30 #k_ruby
\ ( 祝 )30 回/
自己紹介 森塚 真年 GitHub: @sanfrecce-osaka Twitter: @sanfrecce_osaka Qiita: @sanfrecce_osaka 株式会社エンペイ
Ruby3.1/Rails6.1 We are hiring!! from 神奈川
String#split
よく知っている
とある日 スペースで分割したかった # よく使っているパターン ' ほげ ふが '.split(/[[:space:]]/) # =>
["", "ほげ", "", "", "", "", "", "", "", "", "ふが"] # 挙動を確認するためirbで実行 ' ほげ ふが '.split(' ') ' ほげ ふが '.split(/ /)
なん・・・だと・・・? ' ほげ ふが '.split(' ') # => ["ほげ", "ふが"]
' ほげ ふが '.split(/ /) # => ["", "ほげ", "", "", "", "", "", "", "", "", "ふが"]
String#split の型シグネチャ def split: (?Regexp | string pattern, ?int limit)
-> Array[String] | (?Regexp | string pattern, ?int limit){ (String) -> void } -> self https://github.com/ruby/rbs/blob/v2.4.0/core/string.rbs#L2660
pattern( 正規表現 ) 通常 正規表現にマッチする部分で分割 括弧によるグルーピングがある場合 グループにマッチした文字列も結果に含む 空文字列にマッチする場合 文字列を1 文字ずつに分割
マルチバイト文字も認識 ' ほげ ふが '.split(/ /) # => ["", "ほげ", "", "", "", "", "", "", "", "", "ふが"] '1-10,20'.split(/([-,])/) # => ["1", "-", "10", ",", "20"] ' a cat '.split(/\s*/) # => ["", "a", "c", "a", "t"]
pattern( 文字列 ) 通常 その文字列自体にマッチする部分で分割 1 バイトの空白文字 先頭と末尾の空白を除く そのうえで空白文字列で分割 空文字列
文字列を1 文字ずつに分割 マルチバイト文字も認識 ',,a,b,c'.split(',') # => ["", "a", "b", "c"] ' a \t b \n c'.split(' ') # => ["a", "b", "c"] ' a cat '.split('') # => ["", "a", "c", "a", "t"]
pattern(nil) default( 厳密には$;) 常に$; で分割 $; もnil の場合 先頭と末尾の空白を除く そのうえで空白文字列で分割
" a \t b \n c ".split(nil) # => ["a", "b", "c"] " a \t b \n c ".split # => ["a", "b", "c"] # split(nil) と同じ
limit limit > 0 最大 limit 個の文字列に分割する limit == 0(default)
分割個数制限はなしで、配列末尾の空文字列 を取り除く limit < 0 分割個数の制限はなし 他言語のsplit も同様のインターフェース 動きは異なる "a,b,c,d,e".split(/,/, 3) # => ["a", "b", "c,d,e"] ",a,b,c,,,".split(/,/, 0) # => ["", "a", "b", "c"] ",a,b,c,,,".split(/,/, -1) # => ["", "a", "b", "c", ""]
block(2.6.0 〜 ) 配列を返す代わりに分割した文字列でブロック を呼び出す 参考記事内のベンチマークだとブロックなしの およそ倍の速度 fruits = []
input_str = "apple, mango, potato, banana, cabbage" input_str.split(", ") do |value| fruits << value if is_fruit?(value) end # => "apple, mango, potato, banana, cabbage" fruits # => ["apple", "mango", "banana"] https://techracho.bpsinc.jp/hachi8833/2018_07_31/59885
おまけ (C のコード ) https://github.com/ruby/ruby/blob/v3_1_2/string.c#L8674
最後に String#split は意外に多彩な動きをする Ruby は奥が深い 便利な処理の裏で色々頑張っている
ご清聴 ありがとうございました