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-about-string-split
Search
Masatoshi Moritsuka
May 19, 2022
Programming
0
45
String#split何もわかっていなかった/didn-t-know-anything-about-string-split
Masatoshi Moritsuka
May 19, 2022
Tweet
Share
More Decks by Masatoshi Moritsuka
See All by Masatoshi Moritsuka
パターンマッチ使ってるかい?(kyobashi.rb)/use-ruby-s-pattern-matching-on-kyobashi-rb
sanfrecce_osaka
0
20
ApplicationController の継承を分割してエラーを減らした話/dividing-application-controller
sanfrecce_osaka
1
120
Input object ではじめる入力値検証/input-value-validation-using-input-object
sanfrecce_osaka
0
270
実例で学ぶRailsアプリケーションデバッグ入門 〜ログインできちゃってました編〜/rails-application-debug-introduction
sanfrecce_osaka
2
600
String#split何もわかっていなかった/didn_t_know_anything_about_string_split
sanfrecce_osaka
0
100
パターンマッチ使ってるかい?/use-ruby-s-pattern-matching?
sanfrecce_osaka
0
570
新しいコミュニティを立ち上げるぞい/launch-new-community
sanfrecce_osaka
0
110
コミュニティ・勉強会の情報収集について/about-collecting-community-information
sanfrecce_osaka
0
77
Ruby2.7の機能を使いたくてruby-next使ってみた/try-using-ruby-next
sanfrecce_osaka
0
99
Other Decks in Programming
See All in Programming
Compiling Python to WebAssembly with py2wasm
syrusakbary
0
130
LLMチャットボットのアプリケーション設計Tips
os1ma
4
660
object-oriented-conference-2024
fuwasegu
6
1.9k
チームでモデリングを育てるうえで 考えたこと・気づいたこと / Cultivating Modeling in Teams: Thoughts and Insights
mackey0225
5
2.3k
プロンプトエンジニアリング入門
tomokusaba
2
970
Deep Dive 大規模システムアーキテクチャ/開発組織エンジニアリング / Deep Dive Large-Scale System Architecture, Development Organization Engineering
nrslib
15
2.9k
DDD, necessary but insufficient: physical design principles for microservices
cer
PRO
0
2.2k
App Router への移行は「改善」となり得るのか?/ Can migration to App Router be an improvement
takefumiyoshii
1
120
BuefyのMaintainerを引き継いだ件
kikuomax
0
520
Kotlinを用いたDSL的な設計手法と使用上の注意
kohii00
3
530
Open Source Swiftc Workshop
kitasuke
1
180
品質とスピードを両立: TypeScriptの柔軟な型システムをバックエンドで活用する
kosui
4
880
Featured
See All Featured
StorybookのUI Testing Handbookを読んだ
zakiyama
10
4.5k
In The Pink: A Labor of Love
frogandcode
137
21k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
501
140k
Docker and Python
trallard
33
2.6k
Building Better People: How to give real-time feedback that sticks.
wjessup
350
18k
Debugging Ruby Performance
tmm1
68
11k
Visualization
eitanlees
135
14k
It's Worth the Effort
3n
180
27k
Done Done
chrislema
178
15k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
111
35k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
225
51k
Happy Clients
brianwarren
91
6.3k
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 は奥が深い 便利な処理の裏で色々頑張っている
ご清聴 ありがとうございました