Ruby에서 부모의 클래스 이름을 얻는 방법
내가 클래스가 A
있고 B
어디에 B
상속 한다고 가정합시다 A
. 부모 클래스 이름을 어떻게 인쇄합니까?B
class A
end
class B < A
end
내가 시도한 몇 가지
>> B.new.class #=> B #which is correct
>> B.new.parent #=> Undefined method `parent`
>> B.parent #=> Object
>> B.parent.class #=> Class
감사 :)
class A
end
class B < A
end
B.superclass # => A
B.superclass.name # => "A"
전체 조상 스택을 원하면 다음을 시도하십시오.
object.class.ancestors
예를 들면 :
> a = Array.new
=> []
> a.class.ancestors
=> [Array, Enumerable, Object, Kernel, BasicObject]
객체 (Instantiated Class)가 주어지면 부모 클래스를 파생시킬 수 있습니다.
>> x = B.new
>> x.class.superclass.name
=>"A"
경우 구글은 레일에서 일하고있어 여기 사람이, 어떤 대신 할 수 있습니다 제공 base_class
뿐만 superclass
아니라 액티브 상속 구조를 탐색합니다.
class A < ActiveRecord::Base
end
class B < A
end
> A.superclass
=> ActiveRecord::Base
> B.superclass
=> A
> A.base_class
=> A
> B.base_class
=> A
더 나아가 ...
class C < B
end
> C.base_class
=> A
In other words, base_class
gives you the top of the inheritance tree but limited to the context of your application. Fair warning though, as far as Rails is concerned "your application" includes any gems you're using, so if you have a model that subclasses something defined in a gem, base_class
will return the gem's class, not yours.
The term you're looking for is superclass
. And indeed you can do B.superclass
to get A
. (You can also do B.ancestors
to get a list of all the classes and modules it inherits from — something like [B, A, Object, Kernel, BasicObject]
.)
Inheritance is a relation between two classes. Inheritance create a parent child relationship between classes. It is a mechanism for code reuse and to allow independent extensions of the original software via public classes and interfaces.The benefit of inheritance is that classes lower down the hierarchy get the features of those higher up, but can also add specific features of their own.
In Ruby, a class can only inherit from a single other class. (i.e. a class can inherit from a class that inherits from another class which inherits from another class, but a single class can not inherit from many classes at once). The BasicObject class is the parent class of all classes in Ruby. Its methods are therefore available to all objects unless explicitly overridden.
Ruby overcome the single class inheritance at once by using the mixin.
I will try to explain with an example.
module Mux
def sam
p "I am an module"
end
end
class A
include Mux
end
class B < A
end
class C < B
end
class D < A
end
You can trace by using class_name.superclass.name and do this process unless you found BasicOject in this hierarchy. BasicObject is super class o every classes. lets see suppose we want to see class C hierarchy tree.
C.superclass
=> B
B.superclass
=> A
A.superclass
=> Object
Object.superclass
=> BasicObject
You can see the whole hierarchy of class C. Point to note using this approach you will not find modules that are included or prepended in the parent classes.
There is another approach to find complete hierarchy including modules. According to Ruby doc ancestors. Returns a list of modules included/prepended in mod (including mod itself).
C.ancestors
=> [C, B, A, Mux, Object, Kernel, BasicObject]
Here, Mux and Kernel are Modules.
http://rubylearning.com/satishtalim/ruby_inheritance.html https://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)
참고URL : https://stackoverflow.com/questions/14778816/how-do-i-get-the-parents-class-name-in-ruby
'Programing' 카테고리의 다른 글
SVG 캔버스를 로컬 파일 시스템에 저장하는 방법 (0) | 2020.10.19 |
---|---|
그래프 API에서 "실제"Facebook 프로필 사진 URL 가져 오기 (0) | 2020.10.19 |
Java I / O에서 "스트림"과 "버퍼"는 정확히 무엇을 의미합니까? (0) | 2020.10.18 |
Android 머티리얼 디자인 버튼-사전 롤리팝 (0) | 2020.10.18 |
jQuery에서 양식 변경을 어떻게 처리합니까? (0) | 2020.10.18 |