Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Ruby as Shell script
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
Hayato Kawai
April 24, 2024
800
2
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Ruby as Shell script
「Gotanda.rb#58」で発表した LT スライドです。
https://gotanda-rb.connpass.com/event/315058/
Hayato Kawai
April 24, 2024
More Decks by Hayato Kawai
See All by Hayato Kawai
Elasticsearch on EKS から Amazon OpenSearch Service にダウンタイムなしで移行した話
fohte
0
93
ALB ログから Trace を気合で繋げる技術
fohte
7
960
トークン対パフォーマンスを最大化しよう
fohte
5
1.6k
Amazon Bedrock のコスト内訳を気合でトラッキングする
fohte
0
120
Bedrock のコスト監視設計
fohte
2
1k
Data Access Control で実現するより細かい権限制御
fohte
0
620
Datadog が支える Wantedly のシステム運用
fohte
0
210
Devin や Cursor などの生成 AI ツール導入に向けて何を取り組んだのか
fohte
2
460
Datadog Network Monitoring を活用して NAT Gateway 課金を 80 % 削減した話
fohte
2
950
Featured
See All Featured
Automating Front-end Workflow
addyosmani
1370
210k
The SEO identity crisis: Don't let AI make you average
varn
0
620
What the history of the web can teach us about the future of AI
inesmontani
PRO
2
770
Information Architects: The Missing Link in Design Systems
soysaucechin
1
1.2k
Reality Check: Gamification 10 Years Later
codingconduct
0
2.4k
Claude Code どこまでも/ Claude Code Everywhere
nwiizo
68
60k
Java REST API Framework Comparison - PWX 2021
mraible
34
9.8k
Odyssey Design
rkendrick25
PRO
2
910
The Straight Up "How To Draw Better" Workshop
denniskardys
239
140k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
37
6.6k
Gemini Prompt Engineering: Practical Techniques for Tangible AI Outcomes
mfonobong
2
610
Jess Joyce - The Pitfalls of Following Frameworks
techseoconnect
PRO
1
460
Transcript
Ruby as Shell script Gotanda.rb#58 2024-04-24 - Hayato Kawai (@fohte)
あなた誰 名前: @fohte (ふぉーて) 川井 颯人 (Hayato Kawai) 所属: ウォンテッドリー株式会社
趣味: 🎮 🎹
シェルスクリプト 書いてますか?
シェルスクリプト つらい
シェルスクリプトはつらい • nc コマンドが OS によって 若干違う ◦ 条件分岐の嵐 https://speakerdeck.com/fohte/
rubocop-daemon-li-hua-oss-noku-nao
Ruby で書こう
シェルスクリプトを書きたくなる場面 • 一度しか使わないが、やや複雑な処理 ◦ e.g. 何かを集計するバッチ • 日常的なタスクの自動化 ◦ e.g.
Git hooks、リポジトリ固有の処理 (初期セットアップなど)
シェルスクリプト (bash) はここがつらい • コマンドに環境差異がある ◦ よくある例: sed コマンドは macOS
でも BSD 版/GNU 版で動作が異なる ▪ デフォルトでは BSD 版、gnu-sed を入れていると GNU 版
シェルスクリプト (bash) はここがつらい • syntax が難しい ◦ if [ -n
"${1:-}"]; then … ▪ -n 🤔 • !$1.empty? と同じ ▪ ${1:-} 🤔 🤔 • defined?($1) ? $1 : '' と同じ
シェルスクリプト (bash) はここがつらい • ハマりどころが多い ◦ よくある例: 途中でコマンドが失敗しても処理が続行される ▪ set
-eo pipefail しよう、みたいなお作法はあるが、知らないとハマる ◦ 未定義変数を参照してもエラーにならない ▪ set -u すれば解決するが、 …
Ruby で書けば解決! • コマンドに環境差異がある => Ruby のメソッドや標準ライブラリを使おう • syntax が難しい
=> Ruby は平易 (ですよね?) • ハマりどころが多い => Ruby なら慣れてる (人も多いと思いたい)
Ruby で簡単なスクリプトを書くときのお作法 お作法はたった 2 つ • shebang (#!/usr/bin/env ruby) を書こう
• bundler/inline を使おう
shebang (#!/usr/bin/env ruby) を書こう • #!/usr/bin/env ruby をファイルの 1 行目に書く
◦ 簡単にいうと「Ruby で実行する」という定義になる ▪ #!/bin/bash なら Bash、#!/usr/bin/env python なら Python
shebang の何が嬉しいのか • ruby script.rb みたいに書かなくても ./script だけで実行できるようになる ◦ (script
というファイル名 & ファイルに実行権限がある前提 ) ◦ 実行する人にとって、「そのスクリプトが何で実装されているのか」を 意識しなくて良くなる • Git hooks も Ruby で実装できる ◦ 例えば pre-commit は .git/hooks/pre-commit というパス ◦ Ruby で書いて shebang 書いてこのパスに置けば良い
bundler/inline を使おう • よくあるケース: Ruby スクリプトの中からも gem を使いたい! ◦ 例:
Faraday 使いたいな… • そんなときに便利なのが bundler/inline
gem 使いたいときに簡単なのは gem install • 一番簡単なのは gem install すること ◦
gem install ではシステム全体に gem をインストールする ◦ require で読み込めるようになる • ただし他の環境で実行できなくなる ◦ 実行前に gem install しておく必要がある
そこで bundler/inline が便利 • bundler/inline を使えば解決!
bundler/inline の便利なところ • 実行時に bundle install される ◦ Gemfile 不要
◦ gem がなければインストールされるし、あればされない ◦ 実行時に gem があることが保証される ▪ gem install しておく必要がなくなる • 記法は Gemfile と同じ
余談: コマンドライン引数を定義したい ! optparse が便利 (標準ライブラリ) https://docs.ruby-lang.org/ja/latest/library/optparse.html