Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Speaker Deck
PRO
Sign in
Sign up for free
String#split何もわかっていなかった/didn-t-know-anything-about-string-split
Masatoshi Moritsuka
May 19, 2022
Programming
0
21
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
String#split何もわかっていなかった/didn_t_know_anything_about_string_split
sanfrecce_osaka
0
26
パターンマッチ使ってるかい?/use-ruby-s-pattern-matching?
sanfrecce_osaka
0
130
新しいコミュニティを立ち上げるぞい/launch-new-community
sanfrecce_osaka
0
31
コミュニティ・勉強会の情報収集について/about-collecting-community-information
sanfrecce_osaka
0
52
Ruby2.7の機能を使いたくてruby-next使ってみた/try-using-ruby-next
sanfrecce_osaka
0
68
業務のコード以外でRubyを書く/#write-ruby-other-than-business-code
sanfrecce_osaka
2
95
読みやすいコードを書くために気をつけていること/ be-careful-when-writing-readable-code
sanfrecce_osaka
0
140
Procのススメ/recommendation-of-proc
sanfrecce_osaka
0
3k
プログラミングの勉強について考えてみる/Think of programming learning
sanfrecce_osaka
2
470
Other Decks in Programming
See All in Programming
Reactは何を提供するLibraryなのか?
taro28
3
450
データ分析やAIの "運用" について考える
mmorito
0
150
プロダクトのタイプ別 GraphQL クライアントの選び方
shozawa
0
5.5k
IE Graduation Certificate
jxck
6
4.8k
マルチプロダクト×非構造化データ×機械学習を支えるデータ信頼性
akino
0
150
Haskellでオブジェクト指向プログラミング
koheisakata
0
120
1時間半で克服するJavaScriptの非同期処理/async_javascript_kokufuku
marchin1989
2
620
インターン生・新卒向け、学校でもっと教えてほしいITエンジニア基本スキル
nearme_tech
0
130
Cybozu GoogleI/O 2022 LT会 - Input for all screens
jaewgwon
0
360
Lancersをコンテナへ本番移行する取り組み
rvirus0817
1
400
CakePHPの内部実装 から理解するPSR-7
boro1234
0
250
Reactive Java Microservices on Kubernetes with Spring and JHipster
deepu105
1
170
Featured
See All Featured
Docker and Python
trallard
27
1.6k
What the flash - Photography Introduction
edds
62
10k
The Invisible Customer
myddelton
110
11k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
498
130k
GitHub's CSS Performance
jonrohan
1020
420k
Support Driven Design
roundedbygravity
86
8.5k
Why You Should Never Use an ORM
jnunemaker
PRO
47
7.6k
Art Directing for the Web. Five minutes with CSS Template Areas
malarkey
196
9.4k
Stop Working from a Prison Cell
hatefulcrawdad
261
17k
Designing the Hi-DPI Web
ddemaree
272
32k
Debugging Ruby Performance
tmm1
65
10k
4 Signs Your Business is Dying
shpigford
169
20k
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 は奥が深い 便利な処理の裏で色々頑張っている
ご清聴 ありがとうございました