This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
anonymous_class = Class.new(String) do | |
def hello(name) | |
"Hello, #{name}" | |
end | |
end | |
ex_class = anonymous_class.new | |
p ex_class.hello("murajun1978") # => murajun1978 | |
p anonymous_class.name # => nil |
Classクラスの引数にクラスを渡すと継承と同じことができる
これと同じ
class Foo < String; end
11行目でクラス名を取得してるけど、nilなので名無しです
ちゃんと名前を付けてあげましょう
Foo = anonymous_class p anonymous_class.name # => Foo
親クラスを確認
p anonymous_class.superclass # => String p Foo.superclass # => String
helloメソッドの有無を確認
p Foo.instance_methods.grep(/hello/) # => [:hello]
( ̄(エ) ̄)彡☆