Created
January 25, 2016 13:52
-
-
Save taisyo7333/29acfba540ce99d81d19 to your computer and use it in GitHub Desktop.
Ruby Class
This file contains hidden or 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
# encoding: SJIS | |
class Foo | |
def initialize(a) | |
@a = a | |
end | |
def method1 | |
@a | |
end | |
end | |
class FooExt < Foo | |
def initialize(a,b) | |
@b = b | |
super a | |
end | |
def method2(c) | |
@a + @b + c | |
end | |
end | |
p "Foo.ancestors:", Foo.ancestors | |
p "FooExt.ancestors:", FooExt.ancestors | |
# 継承関係を判定する | |
p "Foo < Object" , Foo < Object | |
p "Foo > Object" , Foo > Object | |
# オブジェクトがもつインスタンスメソッド | |
# false : superclassを辿らない | |
p Foo.instance_methods(false) | |
p FooExt.instance_methods(false) | |
# | |
p FooExt.instance_methods(true) | |
foo1 = Foo.new(1) | |
p foo1.instance_variables | |
fooE = FooExt.new(1,2) | |
p fooE.instance_variables | |
This file contains hidden or 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
>ruby Code4-7.rb | |
ruby Code4-7.rb | |
"Foo.ancestors:" | |
[Foo, Object, Kernel, BasicObject] | |
"FooExt.ancestors:" | |
[FooExt, Foo, Object, Kernel, BasicObject] | |
"Foo < Object" | |
true | |
"Foo > Object" | |
false | |
[:method1] | |
[:method2] | |
[:method2, :method1, :nil?, :===, :=~, :!~, :eql?, :hash, :<=>, :class, :singleton_class, :clone, :dup, :taint, :tainted?, :untaint, :untrust, :untrusted?, :trust, :freeze, :frozen?, :to_s, :inspect, :methods, :singleton_methods, :protected_methods, :private_methods, :public_methods, :instance_variables, :instance_variable_get, :instance_variable_set, :instance_variable_defined?, :remove_instance_variable, :instance_of?, :kind_of?, :is_a?, :tap, :send, :public_send, :respond_to?, :extend, :display, :method, :public_method, :singleton_method, :define_singleton_method, :object_id, :to_enum, :enum_for, :==, :equal?, :!, :!=, :instance_eval, :instance_exec, :__send__, :__id__] | |
[:@a] | |
[:@b, :@a] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment