Slide 1

Slide 1 text

My Gems for AtCoder My Gems for AtCoder 大澤広朗 X:@qwyngg GitHub:@QWYNG Roppongi.rb # 20 Powered by Rabbit 3.0.3 and COZMIXNG

Slide 2

Slide 2 text

今日の目的 自作Gem2つ宣伝をします! GreenDay IrbRemote 私がAtCoderという競技プログラミングサービ スに参加するときに使っているGemです

Slide 3

Slide 3 text

GreenDay

Slide 4

Slide 4 text

GreenDay https://github.com/QWYNG/green_day AtCoderの問題からテストを自動生成してくれるGem

Slide 5

Slide 5 text

AtCoderの問題ページ 問題ページには入力例と出力例が載っています

Slide 6

Slide 6 text

GreenDayのデモ(1) $ bundle exec green_day new abc150 abc150 ├── A.rb ├── B.rb ├── C.rb ├── D.rb ├── E.rb ├── F.rb └── spec ├── A_spec.rb ├── B_spec.rb ├── C_spec.rb ├── D_spec.rb ├── E_spec.rb └── F_spec.rb

Slide 7

Slide 7 text

GreenDayのデモ(2) RSpec.describe 'abc150/A.rb' do it 'test with "2 900\n"' do io = IO.popen('ruby abc150/A.rb', 'w+') io.puts("2 900\n") io.close_write expect(io.readlines.join).to eq("Yes\n") end it 'test with "1 501\n"' do io = IO.popen('ruby abc150/A.rb', 'w+') io.puts("1 501\n") io.close_write expect(io.readlines.join).to eq("No\n") end it 'test with "4 2000\n"' do io = IO.popen('ruby abc150/A.rb', 'w+') io.puts("4 2000\n") io.close_write expect(io.readlines.join).to eq("Yes\n") end end

Slide 8

Slide 8 text

GreenDayのデモ(3) 解答コードを書いて… # abc150/A.rb k, x = gets.split.map(&:to_i) puts k * 500 >= x ? 'Yes' : 'No'

Slide 9

Slide 9 text

GreenDayのデモ(4) 問題ページの入力例と出力例を使ってテスト が実行できる! $ rspec abc150/spec/A_spec.rb ... Finished in 0.3589 seconds (files took 0.11844 seconds to load) 3 examples, 0 failures

Slide 10

Slide 10 text

IrbRemote

Slide 11

Slide 11 text

IrbRemote 別プロセスで起動しているIRBを操作できるよ うになるGem

Slide 12

Slide 12 text

IrbRemoteのデモ(1) # sample.rb require 'irb-remote' k = 1 ; x = 500 check_completion = '補完確認用変数' binding.irb_remote puts k * 500 >= x ? 'Yes' : 'No'

Slide 13

Slide 13 text

IrbRemoteのデモ(2) $ ruby sample.rb [irb-remote] Waiting for client on druby://127.0.0.1:9876

Slide 14

Slide 14 text

IrbRemoteのデモ(3) $ irb-remote # 別の仮想端末で実行 Connected to remote session on druby://127.0.0.1:9876 From: sample.rb @ line 5 : 1: require 'irb-remote' 2: k = 1 ; x = 500 3: check_completion = '補完確認用変数' 4: => 5: binding.irb_remote 6: 7: puts k * 500 >= x ? 'Yes' : 'No' irb-remote> check_completion check_completionPress Option+d to read the full document String < Object ---------------------------------------- Includes: Comparable (from ruby core) Rainbow::Ext::String::InstanceMethods (from gem rainbow-3.1.1) (from ruby core) ----------------------------------------

Slide 15

Slide 15 text

why IrbRemote?(1) なぜIrbRemoteを作ったのか? 入出力をキャプチャしていてもIRBを使いたいから

Slide 16

Slide 16 text

why IrbRemote?(2) GreenDayで生成したテストは回答コードでプ ロセスを起動して入出力をキャプチャしている io = IO.popen('ruby abc150/A.rb', 'w+') expect(io.readlines.join).to eq("Yes\n")

Slide 17

Slide 17 text

why IrbRemote?(3) 解答コード内でIRBを起動するとIRB出力が キャプチャされてしまう > rspec abc150/spec/A_spec.rb 1) abc150/A.rb test with "2 900\n" Failure/Error: expect(io.readlines.join).to eq("Yes\n") Diff: @@ -1,9 +1,18 @@ + +From: abc150/A.rb @ line 3 : + + 1: k, x = gets.split.map(&:to_i) + 2: check_complition = '予測変換もいい感じになってほしいなぁ' + => 3: binding.irb + 4: puts k * 500 >= x ? 'Yes' : 'No' +Switch to inspect mode. + Yes

Slide 18

Slide 18 text

why IrbRemote?(4) IrbRemoteを使うと、入出力をキャプチャしつつ IRBを使うことができる

Slide 19

Slide 19 text

IrbRemoteの仕組み dRubyを使ってオブジェクトをプロセス間でや りとりしています dRubyでREPLを行う発想は先人の方を参考に したものです https://github.com/Mon-Ouie/pry-remote https://github.com/iguchi1124/irb_remote 今回はRelineを使いたかったので自作しました

Slide 20

Slide 20 text

まとめ 今日は自作Gem2つを紹介しました GreenDay AtCoderの問題と入出力例からテストを自動生成してくれるGem IrbRemote 別プロセスで起動しているIRBを操作できるGem 皆さんもぜひ使ってみてください Powered by Rabbit 3.0.3 and COZMIXNG