質問編集履歴
3
Test3.rbが呼び出せるように変更
title
CHANGED
File without changes
|
body
CHANGED
@@ -12,12 +12,12 @@
|
|
12
12
|
@password = 'password'
|
13
13
|
@username = 'hoge'
|
14
14
|
@dataBaseName = 'databaseName'
|
15
|
-
@@
|
15
|
+
@@client = connect
|
16
16
|
end
|
17
17
|
|
18
18
|
private
|
19
19
|
def connect
|
20
|
-
|
20
|
+
client = Mysql2:Client.new(host: @hostname ,username: @username, password: @password , database: @dataBaseName)
|
21
21
|
end
|
22
22
|
end
|
23
23
|
|
@@ -26,12 +26,8 @@
|
|
26
26
|
class Test2 < Test1
|
27
27
|
def initialize
|
28
28
|
super
|
29
|
-
@@
|
29
|
+
@@client.query('set character set utf8') ←該当箇所
|
30
30
|
end
|
31
|
-
|
32
|
-
def connClose
|
33
|
-
@@connection.close
|
34
|
-
end
|
35
31
|
end
|
36
32
|
|
37
33
|
|
@@ -39,10 +35,12 @@
|
|
39
35
|
require 'mysql2'
|
40
36
|
require 'Test1'
|
41
37
|
require 'Test2'
|
38
|
+
|
39
|
+
doMysql
|
40
|
+
|
42
41
|
def doMysql
|
43
42
|
com = Test2.new()
|
44
43
|
中間処理は省略
|
45
|
-
com.connClose
|
46
44
|
end
|
47
45
|
```
|
48
46
|
|
2
エラー発生箇所を追記
title
CHANGED
File without changes
|
body
CHANGED
@@ -26,7 +26,7 @@
|
|
26
26
|
class Test2 < Test1
|
27
27
|
def initialize
|
28
28
|
super
|
29
|
-
@@connection.query('set character set utf8')
|
29
|
+
@@connection.query('set character set utf8') ←該当箇所
|
30
30
|
end
|
31
31
|
|
32
32
|
def connClose
|
@@ -50,6 +50,8 @@
|
|
50
50
|
このようなファイルの構成でdb接続を考えております。
|
51
51
|
初回の接続などはうまくいくのですが、2回目などでundefine method 'query' for nil:NilClassでエラーが発生します。
|
52
52
|
|
53
|
+
エラー発生箇所 Test2.rbの5行目
|
54
|
+
|
53
55
|
確かに@@connectionをtest2.rbのsuperの下でログ出力したところ、nilが帰ってきました。
|
54
56
|
|
55
57
|
一度は繋がるが、二度目以降で繋がらない理由がわかりません。
|
1
クラス名の頭文字を大文字に変更 エラー発生箇所に該当箇所と書いてあるところを太文字に変更 test3.rbにrequireを追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -3,10 +3,10 @@
|
|
3
3
|
dbの接続のインスタンスについて
|
4
4
|
|
5
5
|
```ruby
|
6
|
-
|
6
|
+
Test1.rb
|
7
7
|
require 'mysql2'
|
8
8
|
|
9
|
-
class
|
9
|
+
class Test1
|
10
10
|
def initialize
|
11
11
|
@hostname = 'hostname'
|
12
12
|
@password = 'password'
|
@@ -21,12 +21,12 @@
|
|
21
21
|
end
|
22
22
|
end
|
23
23
|
|
24
|
-
|
24
|
+
Test2.rb
|
25
25
|
require 'mysql2'
|
26
|
-
class
|
26
|
+
class Test2 < Test1
|
27
27
|
def initialize
|
28
28
|
super
|
29
|
-
@@connection.query('set character set utf8') ←該当箇所
|
29
|
+
@@connection.query('set character set utf8') **←該当箇所**
|
30
30
|
end
|
31
31
|
|
32
32
|
def connClose
|
@@ -37,9 +37,10 @@
|
|
37
37
|
|
38
38
|
test3.rb
|
39
39
|
require 'mysql2'
|
40
|
-
|
40
|
+
require 'Test1'
|
41
|
+
require 'Test2'
|
41
42
|
def doMysql
|
42
|
-
com =
|
43
|
+
com = Test2.new()
|
43
44
|
中間処理は省略
|
44
45
|
com.connClose
|
45
46
|
end
|