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
Masatoshi Moritsuka
May 19, 2022
Programming
230
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
String#split何もわかっていなかった/didn_t_know_anything_about_string_split
Masatoshi Moritsuka
May 19, 2022
More Decks by Masatoshi Moritsuka
See All by Masatoshi Moritsuka
拙者、『型は欲しいが型は書きたくない』者たちとの和睦を結び、るびぃにおける型の領地安堵を実現せんと欲す者也 #sekigahara01/sekigahara01
sanfrecce_osaka
4
2.5k
Rails の CLI ツールの書き方/writing-rails-cli-tool
sanfrecce_osaka
0
67
Time.zone.parse('dark')/time-zone-parse-dark
sanfrecce_osaka
0
120
外部APIが絡むテストをちょっといい感じに書く/a-little-nice-writing-external-api-testing
sanfrecce_osaka
0
38
gem_rbs_collection へのコントリビュートから始める Ruby の型の世界/contributing-gem-rbs-collection
sanfrecce_osaka
0
620
Rails と人魚の話/rails-and-mermaid
sanfrecce_osaka
0
490
パターンマッチ使ってるかい?(kyobashi.rb)/use-ruby-s-pattern-matching-on-kyobashi-rb
sanfrecce_osaka
0
280
ApplicationController の継承を分割してエラーを減らした話/dividing-application-controller
sanfrecce_osaka
1
420
Input object ではじめる入力値検証/input-value-validation-using-input-object
sanfrecce_osaka
0
610
Other Decks in Programming
See All in Programming
JAWS-UG横浜 #102 AWSサ終供養LT会 成仏できない AWS サービスたち 〜本日、三体供養します〜
maroon1st
0
340
生成AI導入の「期待外れ」を乗り越える ー 開発フロー改革が目指す、真の組織変革
starfish719
0
4k
ドリフトを絶対に許さない(?)CDK運用 / CDK Ops with Zero Tolerance for Drifts (?)
akihisaikeda
1
180
AI Engineeringは、AIプロダクトだけのものか? 〜AIがソフトウェアを作る時代の新しい当たり前〜 / No AI in your product. AI Engineering in your development.
rkaga
4
380
使いながら育てる Claude Code — 開発フローの1コマンド化 × 繰り返し指摘の自動仕組み化
shiki_kakaku
0
1.7k
torikago - Ruby::Boxで照らすモジュラモノリスの実行境界
se4weed
1
350
壊れたパーサから始める関数型設計と構成的なパーサ #fp_matsuri
raiga0310
2
450
PHP に部分適用が来るぞ!……ところで何それ?おいしいの? #phpcon / phpcon-2026
shogogg
0
600
VibeCodingからAgenticWorkflowへ
starfish719
0
320
ビデオ通話が繋がる0.2秒で何が起きているのか
supurazako
2
180
Claude Code全社展開のためにやったことn選~プラグイン302個・コミッター271人を支えるために~
kenchan
5
1.4k
Lean は証明の正しさを確認するためだけのツールって思ってませんか?
inoueasei
1
150
Featured
See All Featured
Building Experiences: Design Systems, User Experience, and Full Site Editing
marktimemedia
0
570
Fireside Chat
paigeccino
42
4k
Why You Should Never Use an ORM
jnunemaker
PRO
61
10k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
16
2.1k
HDC tutorial
michielstock
2
790
What the history of the web can teach us about the future of AI
inesmontani
PRO
1
650
Hiding What from Whom? A Critical Review of the History of Programming languages for Music
tomoyanonymous
3
1.1k
A designer walks into a library…
pauljervisheath
211
24k
Color Theory Basics | Prateek | Gurzu
gurzu
0
410
RailsConf 2023
tenderlove
30
1.5k
SEO for Brand Visibility & Recognition
aleyda
0
4.7k
Faster Mobile Websites
deanohume
310
32k
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 は奥が深い 便利な処理の裏で色々頑張っている
ご清聴 ありがとうございました