回答編集履歴
1
サンプルコードの掲載
test
CHANGED
@@ -1,3 +1,65 @@
|
|
1
1
|
`self.kind_of?(A)`
|
2
2
|
|
3
3
|
でクラスを判断できます。
|
4
|
+
|
5
|
+
|
6
|
+
|
7
|
+
```ruby
|
8
|
+
|
9
|
+
#!/usr/bin/env ruby
|
10
|
+
|
11
|
+
# -*- encoding:utf-8 -*-
|
12
|
+
|
13
|
+
#
|
14
|
+
|
15
|
+
|
16
|
+
|
17
|
+
class Base
|
18
|
+
|
19
|
+
def method()
|
20
|
+
|
21
|
+
if self.kind_of?(A)
|
22
|
+
|
23
|
+
p 'This class is A !'
|
24
|
+
|
25
|
+
else
|
26
|
+
|
27
|
+
p 'This class is not A !'
|
28
|
+
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
|
35
|
+
|
36
|
+
|
37
|
+
class A < Base
|
38
|
+
|
39
|
+
end
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
class B < Base
|
44
|
+
|
45
|
+
end
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
a = A.new
|
50
|
+
|
51
|
+
b = B.new
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
array = [a,b]
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
for i in (0...array.length)
|
60
|
+
|
61
|
+
array[i].method();
|
62
|
+
|
63
|
+
end
|
64
|
+
|
65
|
+
```
|