LoginSignup
0
1

More than 5 years have passed since last update.

Ruby ローカル環境でファイル実行

Posted at

結論

処理のみを記述したrubyファイルと与えられる入力値を入れたテキストファイルを作成して

$ ruby 実行ファイル.rb < 入力値ファイル.txt

あらすじ

正直な話「Paiza.ioを使えばいいじゃん」で終わってしまう内容なのですが、数時間ほどググってもやりかたを記載している記事やサイトが見つからなかったので作りました。

提出するコード(を記述する)ファイルを作成

適当にsample.rbなどとして保存。editorで開いたあと、処理を記述。

sample.rb
loop do
  x = gets.split.map(&:to_i)
  break if (x[0] == 0) && (x[1] == 0)

  puts x.sort.join(' ')
end

与えられる入力値のファイルを作成

今回はこちら"AOJ"の問題を例に

text.txt
0 1
1 0
10 10
5758 6838
7515 113
5627 1051
7419 3010
4086 6212
2767 2749
2060 9084
7543 2225
1183 5089
5566 5137
4978 6966
311 495
54 1367
3145 7031
5736 9882
8505 524
2102 8394
0 0

ターミナルにて実行

$ ruby sample.rb < text.txt

以下が実行結果

0 1
0 1
10 10
5758 6838
113 7515
1051 5627
3010 7419
4086 6212
2749 2767
2060 9084
2225 7543
1183 5089
5137 5566
4978 6966
311 495
54 1367
3145 7031
5736 9882
524 8505
2102 8394

あんなに調べたのにたったこれだけだなんて・・。
なんでFile.open系の記事ばかりなの・・。

0
1
4

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1