ruby_learner 1.2.0 → 1.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/contents/questions/sequential_check/section_10/part_1/lib/answer.rb +5 -7
- data/contents/questions/sequential_check/section_10/part_1/lib/sentence.org +10 -8
- data/contents/questions/sequential_check/section_10/part_1/lib/workplace.rb +1 -1
- data/contents/questions/sequential_check/section_10/part_1/spec/workplace_spec.rb +4 -4
- data/contents/questions/sequential_check/section_10/part_2/lib/answer.rb +6 -7
- data/contents/questions/sequential_check/section_10/part_2/lib/sentence.org +9 -7
- data/contents/questions/sequential_check/section_10/part_2/lib/workplace.rb +1 -1
- data/contents/questions/sequential_check/section_10/part_2/spec/workplace_spec.rb +4 -9
- data/contents/questions/sequential_check/section_10/part_3/lib/answer.rb +12 -0
- data/contents/questions/sequential_check/section_10/part_3/lib/sentence.org +8 -0
- data/contents/questions/sequential_check/section_10/part_3/lib/workplace.rb +1 -0
- data/contents/questions/sequential_check/{section_6/part_4 → section_10/part_3}/spec/spec_helper.rb +0 -0
- data/contents/questions/sequential_check/section_10/part_3/spec/workplace_spec.rb +10 -0
- data/contents/questions/sequential_check/section_2/foo/nbar/nbaz/n +0 -0
- data/contents/questions/sequential_check/section_2/part_3/lib/answer.rb +3 -6
- data/contents/questions/sequential_check/section_2/part_3/lib/sentence.org +2 -0
- data/contents/questions/sequential_check/section_2/part_3/lib/workplace.rb +0 -4
- data/contents/questions/sequential_check/section_2/part_3/spec/workplace_spec.rb +8 -7
- data/contents/questions/sequential_check/section_3/part_2/lib/answer.rb +3 -5
- data/contents/questions/sequential_check/section_3/part_2/lib/workplace.rb +0 -4
- data/contents/questions/sequential_check/section_3/part_2/spec/workplace_spec.rb +8 -7
- data/contents/questions/sequential_check/section_4/part_2/lib/answer.rb +2 -2
- data/contents/questions/sequential_check/section_4/part_2/lib/workplace.rb +1 -1
- data/contents/questions/sequential_check/section_4/part_3/lib/answer.rb +8 -10
- data/contents/questions/sequential_check/section_4/part_3/lib/sentence.org +1 -0
- data/contents/questions/sequential_check/section_4/part_3/lib/workplace.rb +1 -3
- data/contents/questions/sequential_check/section_4/part_3/spec/workplace_spec.rb +6 -7
- data/contents/questions/sequential_check/section_6/part_2/lib/answer.rb +9 -4
- data/contents/questions/sequential_check/section_6/part_2/lib/sentence.org +35 -2
- data/contents/questions/sequential_check/section_6/part_2/lib/workplace.rb +2 -0
- data/contents/questions/sequential_check/section_6/part_2/spec/workplace_spec.rb +9 -10
- data/contents/questions/sequential_check/section_6/part_3/lib/answer.rb +5 -5
- data/contents/questions/sequential_check/section_6/part_3/lib/sentence.org +44 -5
- data/contents/questions/sequential_check/section_6/part_3/lib/workplace.rb +3 -0
- data/contents/questions/sequential_check/section_6/part_3/spec/workplace_spec.rb +2 -2
- data/contents/questions/sequential_check/section_7/part_1/lib/answer.rb +5 -3
- data/contents/questions/sequential_check/section_7/part_1/lib/sentence.org +22 -16
- data/contents/questions/sequential_check/section_7/part_1/spec/workplace_spec.rb +8 -8
- data/contents/questions/sequential_check/section_7/part_2/lib/answer.rb +6 -6
- data/contents/questions/sequential_check/section_7/part_2/lib/sentence.org +23 -22
- data/contents/questions/sequential_check/section_7/part_2/spec/workplace_spec.rb +11 -5
- data/contents/questions/sequential_check/section_7/part_3/lib/answer.rb +13 -0
- data/contents/questions/sequential_check/section_7/part_3/lib/sentence.org +24 -0
- data/contents/questions/sequential_check/section_7/part_3/lib/workplace.rb +4 -0
- data/contents/questions/sequential_check/section_7/part_3/spec/spec_helper.rb +100 -0
- data/contents/questions/sequential_check/section_7/part_3/spec/workplace_spec.rb +15 -0
- data/contents/questions/sequential_check/section_8/part_1/lib/answer.rb +11 -6
- data/contents/questions/sequential_check/section_8/part_1/lib/sentence.org +66 -10
- data/contents/questions/sequential_check/section_8/part_1/lib/workplace.rb +10 -0
- data/contents/questions/sequential_check/section_8/part_1/spec/workplace_spec.rb +28 -9
- data/contents/questions/sequential_check/section_8/part_2/lib/answer.rb +14 -0
- data/contents/questions/sequential_check/section_8/part_2/lib/sentence.org +80 -0
- data/contents/questions/sequential_check/section_8/part_2/lib/workplace.rb +1 -0
- data/contents/questions/sequential_check/section_8/part_2/spec/spec_helper.rb +100 -0
- data/contents/questions/sequential_check/section_8/part_2/spec/workplace_spec.rb +34 -0
- data/contents/questions/sequential_check/section_8/part_3/lib/answer.rb +20 -0
- data/contents/questions/sequential_check/section_8/part_3/lib/sentence.org +80 -0
- data/contents/questions/sequential_check/section_8/part_3/lib/workplace.rb +11 -0
- data/contents/questions/sequential_check/section_8/part_3/spec/spec_helper.rb +100 -0
- data/contents/questions/sequential_check/section_8/part_3/spec/workplace_spec.rb +34 -0
- data/contents/questions/sequential_check/section_9/part_1/lib/answer.rb +9 -4
- data/contents/questions/sequential_check/section_9/part_1/lib/sentence.org +93 -4
- data/contents/questions/sequential_check/section_9/part_1/spec/workplace_spec.rb +14 -5
- data/contents/questions/sequential_check/section_9/part_2/lib/answer.rb +8 -5
- data/contents/questions/sequential_check/section_9/part_2/lib/sentence.org +92 -10
- data/contents/questions/sequential_check/section_9/part_2/spec/workplace_spec.rb +14 -5
- data/contents/questions/sequential_check/section_9/part_3/lib/answer.rb +7 -9
- data/contents/questions/sequential_check/section_9/part_3/lib/sentence.org +93 -4
- data/contents/questions/sequential_check/section_9/part_3/spec/workplace_spec.rb +14 -5
- data/lib/ruby_learner/sequential_main.rb +25 -12
- data/lib/ruby_learner/version.rb +1 -1
- metadata +26 -7
- data/contents/questions/sequential_check/section_6/part_4/lib/answer.rb +0 -9
- data/contents/questions/sequential_check/section_6/part_4/lib/sentence.org +0 -12
- data/contents/questions/sequential_check/section_6/part_4/lib/workplace.rb +0 -1
- data/contents/questions/sequential_check/section_6/part_4/spec/workplace_spec.rb +0 -9
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 338fd543221499c1cd39927680ee4e4e12ee0a43
|
4
|
+
data.tar.gz: d564cd050153231fe58b518e53ff3765de0d41e6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 428f563264d669ac7cf02f6d4773746a7eb178748ad24343079c4f266e54905cc098bafb2ba4b9bdee4f75b241ecc4018afbd533da157fd9dc4210121e8d8b26
|
7
|
+
data.tar.gz: ef1f6747a9cca8312e8e0341a367046b29fecd8d9bebedd2a9a26ad376e66c12eb962a004f866a91336a79a345e70eb44057ac3d976794be283bc6751337f651
|
data/Gemfile.lock
CHANGED
@@ -1,12 +1,14 @@
|
|
1
|
-
|
1
|
+
-課題-
|
2
|
+
1つの文字列を受け取り、文字列と一致するファイル名のテキストデータを表示するプログラムを作成せよ。
|
2
3
|
|
4
|
+
* ファイル操作
|
5
|
+
- ファイル操作にはファイル入出力等のコードからファイルに関する操作ができるメソッドを紹介します.
|
6
|
+
- 例によって,この項目についても様々な機能があるので必要最低限の知識を掲載することにします.
|
3
7
|
|
4
|
-
|
8
|
+
** ファイル読み込み
|
9
|
+
- 使用方法
|
10
|
+
File.read
|
5
11
|
|
6
|
-
|
7
|
-
メソッドで実行したい処理
|
8
|
-
end
|
12
|
+
text = File.read(ファイル名)
|
9
13
|
|
10
|
-
|
11
|
-
|
12
|
-
作成したメソッドは、メソッド名をプログラム中に記述することで実行できる。
|
14
|
+
File.readメソッドは、受け取ったファイル名と同じ名前のファイルのデータを読み込むメソッド。
|
@@ -1 +1 @@
|
|
1
|
-
#
|
1
|
+
# section_9/part_1/workplace.rb
|
@@ -1,10 +1,10 @@
|
|
1
1
|
# -*- coding: utf-8 -*-
|
2
2
|
require "open3"
|
3
3
|
|
4
|
-
RSpec.describe "
|
5
|
-
it '
|
4
|
+
RSpec.describe "ARGV-check" do
|
5
|
+
it 'given "for-read.txt", return 4' do
|
6
6
|
workshop = "#{ENV['HOME']}/.ruby_learner/workshop"
|
7
|
-
stdout, stderr, status = Open3.capture3("ruby #{workshop}/lib/workplace.rb")
|
8
|
-
expect { puts stdout }.to output("
|
7
|
+
stdout, stderr, status = Open3.capture3("ruby #{workshop}/lib/workplace.rb #{workshop}/lib/for-read.txt")
|
8
|
+
expect { puts stdout }.to output("このファイルはファイル読み込み用の練習ファイルです。\n\nむかしむかし、あるところに、おじいさんとおばあさんが住んでいました。\nおじいさんは山へしばかりに、おばあさんは川へせんたくに行きました。\nおばあさんが川でせんたくをしていると、ドンブラコ、ドンブラコと、大きな桃が流れてきました。\n").to_stdout
|
9
9
|
end
|
10
10
|
end
|
@@ -1,8 +1,7 @@
|
|
1
|
-
#
|
2
|
-
#
|
3
|
-
|
4
|
-
|
1
|
+
# section_9/part_2/answer.rb
|
2
|
+
# 9-2-file-open.rb
|
3
|
+
filename = ARGV[0]
|
4
|
+
file = File.open(filename)
|
5
|
+
file.each_line do |line|
|
6
|
+
print line
|
5
7
|
end
|
6
|
-
|
7
|
-
hello(ARGV[0])
|
8
|
-
hello(ARGV[1])
|
@@ -1,13 +1,15 @@
|
|
1
|
-
1
|
1
|
+
1つの文字列を受け取り、文字列と一致するファイル名のテキストデータを1行ずつ読み取り、表示するプログラムを作成せよ。
|
2
2
|
|
3
3
|
|
4
|
-
-
|
4
|
+
- file.each_line
|
5
5
|
|
6
|
-
|
7
|
-
|
6
|
+
file = File.open(ファイル名)
|
7
|
+
file.each_line do |変数|
|
8
|
+
繰り返したい処理
|
8
9
|
end
|
10
|
+
file.close
|
9
11
|
|
10
|
-
|
11
|
-
|
12
|
+
each_lineメソッドはファイルの各行をそれぞれ処理するメソッド。
|
13
|
+
ファイルを1行ずつ読み込み、処理を繰り返す。
|
12
14
|
|
13
|
-
|
15
|
+
open,closeメソッドはそれぞれファイルを読み込むためにファイルを開くメソッド、開いたファイルを閉じるメソッド。
|
@@ -1 +1 @@
|
|
1
|
-
#
|
1
|
+
# section_9/part_2/workplace.rb
|
@@ -1,15 +1,10 @@
|
|
1
1
|
# -*- coding: utf-8 -*-
|
2
2
|
require "open3"
|
3
3
|
|
4
|
-
RSpec.describe "
|
5
|
-
it 'given "
|
4
|
+
RSpec.describe "ARGV-check" do
|
5
|
+
it 'given "for-read.txt", return 4' do
|
6
6
|
workshop = "#{ENV['HOME']}/.ruby_learner/workshop"
|
7
|
-
stdout, stderr, status = Open3.capture3("ruby #{workshop}/lib/workplace.rb
|
8
|
-
expect { puts stdout }.to output("
|
9
|
-
end
|
10
|
-
it 'given "tanaka" and "goro", return "Hello, tanaka.\nHello, goro.\n"' do
|
11
|
-
workshop = "#{ENV['HOME']}/.ruby_learner/workshop"
|
12
|
-
stdout, stderr, status = Open3.capture3("ruby #{workshop}/lib/workplace.rb tanaka goro")
|
13
|
-
expect { puts stdout }.to output("Hello, tanaka.\nHello, goro.\n").to_stdout
|
7
|
+
stdout, stderr, status = Open3.capture3("ruby #{workshop}/lib/workplace.rb #{workshop}/lib/for-read.txt")
|
8
|
+
expect { puts stdout }.to output("このファイルはファイル読み込み用の練習ファイルです。\n\nむかしむかし、あるところに、おじいさんとおばあさんが住んでいました。\nおじいさんは山へしばかりに、おばあさんは川へせんたくに行きました。\nおばあさんが川でせんたくをしていると、ドンブラコ、ドンブラコと、大きな桃が流れてきました。\n").to_stdout
|
14
9
|
end
|
15
10
|
end
|
@@ -0,0 +1 @@
|
|
1
|
+
# section_9/part_3/workplace.rb
|
data/contents/questions/sequential_check/{section_6/part_4 → section_10/part_3}/spec/spec_helper.rb
RENAMED
File without changes
|
@@ -0,0 +1,10 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
require "open3"
|
3
|
+
|
4
|
+
RSpec.describe "ARGV-check" do
|
5
|
+
it 'given 3 "for-read.txt", return 4' do
|
6
|
+
workshop = "#{ENV['HOME']}/.ruby_learner/workshop"
|
7
|
+
stdout, stderr, status = Open3.capture3("ruby #{workshop}/lib/workplace.rb 'ドンブラコ' #{workshop}/lib/for-read.txt")
|
8
|
+
expect { puts stdout }.to output("おばあさんが川でせんたくをしていると、ドンブラコ、ドンブラコと、大きな桃が流れてきました。\n").to_stdout
|
9
|
+
end
|
10
|
+
end
|
File without changes
|
@@ -1,13 +1,14 @@
|
|
1
1
|
require "open3"
|
2
|
-
|
2
|
+
|
3
|
+
workplace = "#{ENV['HOME']}/.ruby_learner/workshop/lib/workplace.rb"
|
3
4
|
|
4
5
|
RSpec.describe "STDIN-check" do
|
5
|
-
it 'given 3, return "3.0\n9.0\n"' do
|
6
|
-
|
7
|
-
expect {
|
6
|
+
it 'given 3\n, return "3.0\n9.0\n"' do
|
7
|
+
stdout, stderr, status = Open3.capture3("ruby #{workplace}", :stdin_data=>"3\n")
|
8
|
+
expect { puts stdout }.to output("3.0\n9.0\n").to_stdout
|
8
9
|
end
|
9
|
-
it 'given 8, return "8.0\n64.0\n"' do
|
10
|
-
|
11
|
-
expect {
|
10
|
+
it 'given 8\n, return "8.0\n64.0\n"' do
|
11
|
+
stdout, stderr, status = Open3.capture3("ruby #{workplace}", :stdin_data=>"8\n")
|
12
|
+
expect { puts stdout }.to output("8.0\n64.0\n").to_stdout
|
12
13
|
end
|
13
14
|
end
|
@@ -1,13 +1,14 @@
|
|
1
1
|
require "open3"
|
2
|
-
|
2
|
+
|
3
|
+
workplace = "#{ENV['HOME']}/.ruby_learner/workshop/lib/workplace.rb"
|
3
4
|
|
4
5
|
RSpec.describe "STDIN-check" do
|
5
|
-
it 'given
|
6
|
-
|
7
|
-
expect {
|
6
|
+
it 'given 3\n6\n9\n, return "3\n6\n9\n[3, 6, 9]\n"' do
|
7
|
+
stdout, stderr, status = Open3.capture3("ruby #{workplace}", :stdin_data=>"3\n6\n9\n")
|
8
|
+
expect { puts stdout }.to output("3\n6\n9\n[\"3\\n\", \"6\\n\", \"9\\n\"]\n").to_stdout
|
8
9
|
end
|
9
|
-
it 'given
|
10
|
-
|
11
|
-
expect {
|
10
|
+
it 'given fizz\nbuzz\n, return "fizz\nbuzz\n[\"fizz\\n\", \"buzz\\n\"]\n"' do
|
11
|
+
stdout, stderr, status = Open3.capture3("ruby #{workplace}", :stdin_data=>"fizz\nbuzz\n")
|
12
|
+
expect { puts stdout }.to output("fizz\nbuzz\n[\"fizz\\n\", \"buzz\\n\"]\n").to_stdout
|
12
13
|
end
|
13
14
|
end
|
@@ -1,2 +1,2 @@
|
|
1
|
-
# section_4/
|
1
|
+
# section_4/part_2/workplace.rb
|
2
2
|
|
@@ -1,13 +1,11 @@
|
|
1
1
|
# section_4/part_3/answer.rb
|
2
2
|
# 4-3-case.rb
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
else
|
11
|
-
print 'else'
|
12
|
-
end
|
3
|
+
country = STDIN.gets.chomp
|
4
|
+
case country
|
5
|
+
when 'Japan' then
|
6
|
+
puts 'こんにちは'
|
7
|
+
when 'USA' then
|
8
|
+
puts 'Hello'
|
9
|
+
else
|
10
|
+
print 'else'
|
13
11
|
end
|
@@ -1,5 +1,4 @@
|
|
1
1
|
require "open3"
|
2
|
-
require "#{ENV['HOME']}/.ruby_learner/workshop/lib/workplace.rb"
|
3
2
|
|
4
3
|
filename = "#{ENV['HOME']}/.ruby_learner/workshop/lib/workplace.rb"
|
5
4
|
check_method = false
|
@@ -15,15 +14,15 @@ RSpec.describe "STDIN-check" do
|
|
15
14
|
expect( check_method ).to eq(true)
|
16
15
|
end
|
17
16
|
it 'given Japan, return "こんにちは\n"' do
|
18
|
-
|
19
|
-
expect {
|
17
|
+
stdout, stderr, status = Open3.capture3("ruby #{filename}", :stdin_data=>"Japan\n")
|
18
|
+
expect { puts stdout }.to output("こんにちは\n").to_stdout
|
20
19
|
end
|
21
20
|
it 'given USA, return "Hello\n"' do
|
22
|
-
|
23
|
-
expect {
|
21
|
+
stdout, stderr, status = Open3.capture3("ruby #{filename}", :stdin_data=>"USA\n")
|
22
|
+
expect { puts stdout }.to output("Hello\n").to_stdout
|
24
23
|
end
|
25
24
|
it 'given Italy, return "else\n"' do
|
26
|
-
|
27
|
-
expect {
|
25
|
+
stdout, stderr, status = Open3.capture3("ruby #{filename}", :stdin_data=>"Italy\n")
|
26
|
+
expect { print "#{stdout}" }.to output("else").to_stdout
|
28
27
|
end
|
29
28
|
end
|
@@ -1,7 +1,12 @@
|
|
1
1
|
# section_6/part_2/answer.rb
|
2
|
-
# 6-2-
|
3
|
-
|
2
|
+
# 6-2-hash.rb
|
3
|
+
address = { name: 'tanaka', age: 22 }
|
4
4
|
|
5
|
-
|
5
|
+
p address
|
6
6
|
|
7
|
-
|
7
|
+
address[:name] = 'suzuki'
|
8
|
+
address[:gender] = :man
|
9
|
+
|
10
|
+
address.each do |key, value|
|
11
|
+
puts "#{key}: #{value}"
|
12
|
+
end
|
@@ -1,5 +1,38 @@
|
|
1
1
|
-課題-
|
2
|
-
|
3
|
-
|
2
|
+
与えられたハッシュ(address)の:nameの要素を"suzuki"に変更し,新たに:gender(キー)を作成し:man(要素)を格納せよ.
|
3
|
+
その後,変更前のハッシュをpを用いて出力せよ.
|
4
|
+
また,変更後のハッシュのキーと要素を全てputsを用いて,「キー: 要素」の形で出力せよ.
|
4
5
|
|
6
|
+
* 配列
|
7
|
+
- 配列とは,いくつかのオブジェクトを順序つきで格納したオブジェクトです.格納されたオブジェクトは要素と呼びます.
|
8
|
+
** 使用方法
|
9
|
+
- 配列作成...配列名 = [オブジェクト,オブジェクト,オブジェクト].オブジェクトは複数格納できるが何も格納しなくても良い.
|
10
|
+
- 要素呼び出し...配列名[index] (このindexとは配列の要素番号である.)
|
11
|
+
- 要素追加...配列 << 新しい要素
|
12
|
+
- 要素番号...配列の要素は0から数える.
|
13
|
+
- 要素変更...配列名[index] = オブジェクト
|
14
|
+
- 配列の大きさ(要素数)...配列名.size
|
5
15
|
|
16
|
+
* ハッシュ
|
17
|
+
- ハッシュとは,いくつかのオブジェクトを文字列やシンボルをキーとして格納したオブジェクトです.格納されたオブジェクトは要素と呼びます.
|
18
|
+
** 使用方法
|
19
|
+
- ハッシュ作成...ハッシュ名 = { キー => オブジェクト,キー => オブジェクト, ...}
|
20
|
+
ex) person = { name: "tanaka", age: 24}
|
21
|
+
(キーには数値や文字列やシンボル等が使用可能)
|
22
|
+
- 要素呼び出し...ハッシュ名[キー]
|
23
|
+
- 要素追加...ハッシュ名[新しいキー] = 新しいオブジェクト
|
24
|
+
- 要素変更...ハッシュ名[キー] = 変更後のオブジェクト
|
25
|
+
- ハッシュの大きさ(要素数)...ハッシュ名.size
|
26
|
+
|
27
|
+
** ハッシュでのeach
|
28
|
+
- ハッシュ.each do |key, value|
|
29
|
+
ハッシュのキーと要素を取得可能
|
30
|
+
end
|
31
|
+
|
32
|
+
* シンボル
|
33
|
+
- 文字列に似たオブジェクトで,Rubyがメソッドなどの名前を識別するためのラベルをオブジェクトにしたものです.
|
34
|
+
- 使用方法...:文字列
|
35
|
+
ex) :hoge (シンボル「:hoge」)
|
36
|
+
:"hoge" (上記と同じ意味)
|
37
|
+
- シンボルと同様のことは文字列でもできます.
|
38
|
+
- シンボルは見た目は文字だが,実際は数字であるので,比較を行いやすい.
|
@@ -1,14 +1,13 @@
|
|
1
1
|
require "open3"
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
expect { puts stdout }.to output("[4, 1, 4, 1, 5]\n").to_stdout
|
3
|
+
workshop = "#{ENV['HOME']}/.ruby_learner/workshop"
|
4
|
+
|
5
|
+
RSpec.describe "hash-check" do
|
6
|
+
it 'return {:name=>"tanaka", :age=>22}\n
|
7
|
+
name: suzuki\n
|
8
|
+
age: 22\n
|
9
|
+
gender: man\n' do
|
10
|
+
stdout, stderr, status = Open3.capture3("ruby #{workshop}/lib/workplace.rb")
|
11
|
+
expect { puts stdout }.to output("{:name=>\"tanaka\", :age=>22}\nname: suzuki\nage: 22\ngender: man\n").to_stdout
|
13
12
|
end
|
14
13
|
end
|