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
210
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
43
Time.zone.parse('dark')/time-zone-parse-dark
sanfrecce_osaka
0
110
外部APIが絡むテストをちょっといい感じに書く/a-little-nice-writing-external-api-testing
sanfrecce_osaka
0
30
gem_rbs_collection へのコントリビュートから始める Ruby の型の世界/contributing-gem-rbs-collection
sanfrecce_osaka
0
580
Rails と人魚の話/rails-and-mermaid
sanfrecce_osaka
0
470
パターンマッチ使ってるかい?(kyobashi.rb)/use-ruby-s-pattern-matching-on-kyobashi-rb
sanfrecce_osaka
0
250
ApplicationController の継承を分割してエラーを減らした話/dividing-application-controller
sanfrecce_osaka
1
400
Input object ではじめる入力値検証/input-value-validation-using-input-object
sanfrecce_osaka
0
600
実例で学ぶRailsアプリケーションデバッグ入門 〜ログインできちゃってました編〜/rails-application-debug-introduction
sanfrecce_osaka
2
920
Other Decks in Programming
See All in Programming
〜バイブコーディングを超えて〜 チームで実験し続けたAI駆動開発
tigertora7571
0
200
Claude CodeでETLジョブ実行テストを自動化してみた
yoshikikasama
0
1.2k
GoogleCloudとterraform完全に理解した
terisuke
1
190
Terraform言語の静的解析 / static analysis of Terraform language
wata727
1
140
SREに優しいTerraform構成 modulesとstateの組み方
hiyanger
2
170
Liberating Ruby's Parser from Lexer Hacks
ydah
2
2.7k
when storing skills in S3 file
watany
3
1.5k
書籍「ユーザーストーリーマッピング」が私のバイブル
asumikam
4
490
When benchmarks go bad - what I learned from measuring performance wrong
hollycummins
0
380
ソースコード→AST→オペコード、の旅を覗いてみる
o0h
PRO
1
130
なぜあなたのコードには「コシ」がないのか?〜AI時代に問う、最後まで美味しい設計と戦略〜 #phpconkagawa / phpconkagawa2026
shogogg
0
150
属人化しないコード品質の作り方_2026.04.07.pdf
muraaano
0
330
Featured
See All Featured
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
52
5.9k
Skip the Path - Find Your Career Trail
mkilby
1
120
The browser strikes back
jonoalderson
0
1k
The Director’s Chair: Orchestrating AI for Truly Effective Learning
tmiket
1
160
Exploring the relationship between traditional SERPs and Gen AI search
raygrieselhuber
PRO
2
3.9k
Darren the Foodie - Storyboard
khoart
PRO
3
3.3k
The Curse of the Amulet
leimatthew05
1
12k
Rebuilding a faster, lazier Slack
samanthasiow
85
9.5k
What's in a price? How to price your products and services
michaelherold
247
13k
Keith and Marios Guide to Fast Websites
keithpitt
413
23k
Why You Should Never Use an ORM
jnunemaker
PRO
61
9.8k
The Cost Of JavaScript in 2023
addyosmani
55
9.9k
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 は奥が深い 便利な処理の裏で色々頑張っている
ご清聴 ありがとうございました