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
Rubyでワンライナー / One-liner on Ruby
Search
mather
March 13, 2020
Programming
0
470
Rubyでワンライナー / One-liner on Ruby
mather
March 13, 2020
Tweet
Share
More Decks by mather
See All by mather
SolidjsでLeacTion!を作り直しました / Rebuilt LeacTion! in Solid.js
mather
0
320
Webフレームワークの功罪 / Advantages and considerable point of Web Frameworks
mather
0
470
LeacTion!のアップデートとプチ勉強会へのいざない / Updates of LeacTion and Petit Meetup
mather
0
480
LeacTion!について / About LeacTion!
mather
0
330
認知と思考パターン / Cognition and Pattern
mather
1
280
「モデル」を考える / Think about "model"
mather
0
380
Shall we make a speech?
mather
0
240
Elmでライフゲーム / LifeGame in Elm
mather
1
1k
Elmで関数型を意識する / Think functionally with Elm
mather
0
420
Other Decks in Programming
See All in Programming
CSC509 Lecture 03
javiergs
PRO
0
330
なぜGoのジェネリクスはこの形なのか? Featherweight Goが明かす設計の核心
ryotaros
7
1k
monorepo の Go テストをはやくした〜い!~最小の依存解決への道のり~ / faster-testing-of-monorepos
convto
2
370
2分台で1500examples完走!爆速CIを支える環境構築術 - Kaigi on Rails 2025
falcon8823
3
3k
The Past, Present, and Future of Enterprise Java
ivargrimstad
0
110
ソフトウェア設計の実践的な考え方
masuda220
PRO
3
460
プロダクト開発をAI 1stに変革する〜SaaS is dead時代で生き残るために〜 / AI 1st Product Development
kobakei
0
490
いま中途半端なSwift 6対応をするより、Default ActorやApproachable Concurrencyを有効にしてからでいいんじゃない?
yimajo
2
330
CSC509 Lecture 01
javiergs
PRO
1
430
Le côté obscur des IA génératives
pascallemerrer
0
120
Local Peer-to-Peer APIはどのように使われていくのか?
hal_spidernight
2
440
iOSエンジニア向けの英語学習アプリを作る!
yukawashouhei
0
180
Featured
See All Featured
Building Better People: How to give real-time feedback that sticks.
wjessup
368
20k
RailsConf 2023
tenderlove
30
1.2k
Gamification - CAS2011
davidbonilla
81
5.5k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
15
1.7k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
23
1.5k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
140
34k
How STYLIGHT went responsive
nonsquared
100
5.8k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
333
22k
Fireside Chat
paigeccino
40
3.7k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
231
53k
The Power of CSS Pseudo Elements
geoffreycrofte
79
6k
jQuery: Nuts, Bolts and Bling
dougneiner
64
7.9k
Transcript
Ruby でワンライナー 2020-03-13 Ruby みやざき #1 桑畑英資
⾃⼰紹介 self_introduction = { name: " 桑畑英資", age: 36, job:
" フリーランスエンジニア", fav_language: %w(Scala Elm Ruby), comment: <<-EOS.gsub(/^ */, ""), ⼤学のときにRuby を使う授業でRuby に触れて慣れ親しむ。 その後にJava などにも触れたが、 オブジェクト指向の⾔語としてはRuby の⽅が好きだった。 更にその後Scala を知ったので(以下略) 今でもツールの⽤途でRuby は触っている。 EOS twitter_id: "@mather314" }
ワンライナーとは One-liner、⼀⾏で動くコード Perl ではよくある話 シェルスクリプトでもよくある話 grep とか、 sed とか、 awk
とか、etc... 最近だと peco とかも使えるから可能性が広がって捗る 例: 空のディレクトリに .gitkeep を作るワンライナー for f in $(find . -type d -empty | grep -v '.git/') ; do echo "$f/.gitkeep" ; done (実際に実⾏するときは echo を touch にする)
Web フレームワークばかり使ってませんか? Ruby は汎⽤プログラミング⾔語です 当然ですがスクリプトとして利⽤できます REPL (irb, pry) mruby など組み込みにも利⽤されている
極端な例として今回はワンライナーを紹介 実は ruby コマンドにはいろいろな便利オプションがありま す -e : ⽂字列をスクリプトとして実⾏する -r :
ライブラリを読み込んで実⾏する (require) -n , -p : while gets; ... ; end のように囲んだ状 態で動作する 「それ Ruby じゃなくてもできるよ!」というツッコミは (知ってるので)無視します
単体で $ ruby -e 'puts "Hello,world!"' Hello,world! $ ruby -e
'puts [*1..10].shuffle.join(", ")' 2, 1, 4, 9, 10, 7, 6, 8, 5, 3
標準⼊⼒と $ cat lorem.txt Lorem ipsum dolor sit amet, consectetur
adipiscing elit, ... $ cat lorem.txt | ruby -e 'puts $<.read.split(" ") .map(&:capitalize).join(" ")' Lorem Ipsum Dolor Sit Amet, Consectetur Adipiscing Elit, ... $< : 標準⼊⼒をファイルとしてアクセスできる read の他、 each , readlines とかも便利。
ライブラリも使って $ seq 1 100 | ruby -r prime -n
-e 'puts $_ if Prime.prime?($_.to_i)' 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
ご清聴ありがとうございました