teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

サンプルコードの掲載

2016/03/04 04:29

投稿

NCC1701
NCC1701

スコア1683

answer CHANGED
@@ -1,2 +1,33 @@
1
1
  `self.kind_of?(A)`
2
- でクラスを判断できます。
2
+ でクラスを判断できます。
3
+
4
+ ```ruby
5
+ #!/usr/bin/env ruby
6
+ # -*- encoding:utf-8 -*-
7
+ #
8
+
9
+ class Base
10
+ def method()
11
+ if self.kind_of?(A)
12
+ p 'This class is A !'
13
+ else
14
+ p 'This class is not A !'
15
+ end
16
+ end
17
+ end
18
+
19
+ class A < Base
20
+ end
21
+
22
+ class B < Base
23
+ end
24
+
25
+ a = A.new
26
+ b = B.new
27
+
28
+ array = [a,b]
29
+
30
+ for i in (0...array.length)
31
+ array[i].method();
32
+ end
33
+ ```