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
one_liner_fizzbuzz
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
maimu
December 07, 2023
Programming
0
170
one_liner_fizzbuzz
maimu
December 07, 2023
Tweet
Share
More Decks by maimu
See All by maimu
Gentoo 1年生 ビルドは終わらない
maimux2x
0
78
Rails 1.0 のコードで学ぶ find_by* と method_missing の仕組み / Learn how find_by_* and method_missing work in Rails 1.0 code
maimux2x
1
1.3k
rails_girls_is_my_gate_to_join_the_ruby_commuinty
maimux2x
0
650
ruby-flip-flop
maimux2x
0
140
before_rails_girls_after_rails_girls
maimux2x
0
820
my_study_of_ruby_method
maimux2x
1
190
about_rails_girls_document_translation
maimux2x
0
6.2k
best_for_fbc
maimux2x
0
78
homemade_service_release_front_and_back
maimux2x
0
460
Other Decks in Programming
See All in Programming
コマンドとリード間の連携に対する脅威分析フレームワーク
pandayumi
1
450
今こそ知るべき耐量子計算機暗号(PQC)入門 / PQC: What You Need to Know Now
mackey0225
3
380
20260127_試行錯誤の結晶を1冊に。著者が解説 先輩データサイエンティストからの指南書 / author's_commentary_ds_instructions_guide
nash_efp
1
970
QAフローを最適化し、品質水準を満たしながらリリースまでの期間を最短化する #RSGT2026
shibayu36
2
4.4k
フルサイクルエンジニアリングをAI Agentで全自動化したい 〜構想と現在地〜
kamina_zzz
0
400
AgentCoreとHuman in the Loop
har1101
5
240
そのAIレビュー、レビューしてますか? / Are you reviewing those AI reviews?
rkaga
6
4.6k
0→1 フロントエンド開発 Tips🚀 #レバテックMeetup
bengo4com
0
570
2026年 エンジニアリング自己学習法
yumechi
0
130
AI Agent の開発と運用を支える Durable Execution #AgentsInProd
izumin5210
7
2.3k
【卒業研究】会話ログ分析によるユーザーごとの関心に応じた話題提案手法
momok47
0
200
Amazon Bedrockを活用したRAGの品質管理パイプライン構築
tosuri13
4
700
Featured
See All Featured
Reflections from 52 weeks, 52 projects
jeffersonlam
356
21k
The Anti-SEO Checklist Checklist. Pubcon Cyber Week
ryanjones
0
66
30 Presentation Tips
portentint
PRO
1
220
Navigating the Design Leadership Dip - Product Design Week Design Leaders+ Conference 2024
apolaine
0
180
Art, The Web, and Tiny UX
lynnandtonic
304
21k
Introduction to Domain-Driven Design and Collaborative software design
baasie
1
580
Abbi's Birthday
coloredviolet
1
4.7k
ラッコキーワード サービス紹介資料
rakko
1
2.3M
Breaking role norms: Why Content Design is so much more than writing copy - Taylor Woolridge
uxyall
0
170
What the history of the web can teach us about the future of AI
inesmontani
PRO
1
430
Technical Leadership for Architectural Decision Making
baasie
1
240
Bridging the Design Gap: How Collaborative Modelling removes blockers to flow between stakeholders and teams @FastFlow conf
baasie
0
450
Transcript
ワンライナーで FizzBuzz 2023/12/07 maimu
自己紹介 名前:maimu Xアカウント:maimux2x 所属:永和システムマネジメント エンジニア歴:4ヶ月 好きなRubyメソッド: Enumerable#map 好きなLinuxコマンド: tee
ワンライナーでFizzBuzz
なぜこのテーマなのか Linuxの勉強で学んだコマンドを使ってみたかった
ワンライナーとは? 1行で書かれたコマンドの組み合わせ
今回のお題
試作 seq 100 | awk '$1%15==0{print "FizzBuzz"}$1%5==0{print "Buzz"}$1%3==0{print "Fizz"}$1%15{print $1}'
コマンドについて seq : 連続した数字の列を出力・表示するコマンド awk : grepにプログラム機能をつけたようなもの(プログラミング言語) $1 : 読み込んだ行の1列目の文字列または数値
結果は・・・?
条件分岐を追加! seq 100 | awk '{ if ($1 % 15
== 0) print "FizzBuzz"; else if ($1 % 3 == 0) print "Fizz"; else if ($1 % 5 == 0) print "Buzz"; else print $1; }'
今度はできた!
他のコマンドも試したい
ebanさんのツイートにまさにシェル芸という模範解答が https://twitter.com/eban/status/830272982303936512 seq 1 100|sed '5~5s/.*/Buzz/;3~3s/[^B]*/Fizz/' 模範解答を見つけてしまった・・・
参考にしつつ自分もやってみる seq 100 | sed -e '3~3s/.*/Fizz/' -e '5~5s/.*/Buzz/' -e
'15~15s/.*/FizzBuzz/'
コマンドについて sed : 指定したファイルをコマンドに従って処理する -e : sedで複数のコマンドを指定する際に使用する s : 指定されたパターンに基づいて置換する
~ : 処理対象の位置を指定するアドレス指定子
できた!
もう1個やってみる seq 100 | sed -e '15~15c\FizzBuzz' -e '3~3c\Fizz' -e
'5~5c\Buzz'
コマンドについて c : 指定した行をテキストに置き換える
できた!
Rubyワンライナーでもやってみた seq 100 | ruby -ne 'if $_.to_i%15==0; puts "FizzBuzz"
elsif $_.to_i%5==0; puts "Buzz" elsif $_.to_i%3==0; puts "Fizz" else puts $_ end'
コマンドについて -e : 引数に記述されたRubyのコードを実行するためのオプション -n : 標準入力の内容を1行ずつ読み込む $_ : -nオプションで読み込んだ文字列が格納される変数
できた!
やってみた結果 • ワンライナーでFizzBuzzを4つ実行できた! ◦ 効率性は完全無視・・・ ◦ Rubyワンライナーは普通にコードを書いた方が分かりやすい・・・ • Linuxコマンドと少し仲良くなれた •
Rubyコマンドのオプションを初めて使った
今後の展望 • シェル・ワンライナー160本ノックをやり切りたい! • アイディアと手段を駆使したワンライナーを書いてみたい
参考文献 • man • 1日1問半年以内に習得 シェル・ワンライナー160本ノック