The two dynamic characteristics of Ruby Way

Source: Internet
Author: User

1 Get the list of defined entities

Ruby's Reflection API enables us to detect classes and objects at runtime. So we'll introduce some of the methods of the definitions in module, Class, and object below.

module modules have a constants method that returns all the constant names in the system, including the class name and module name. The nesting method returns a list of nested modules on the current call point.

Ruby Code

list = math.constants # ["E", "PI"]

Module#ancestors returns all the contained classes or modules of the specified class or module.

Ruby Code

List = array.ancestors
# [Array, Enumerable, Object, Kernel]

The Class_variables method returns a table of all the class variables for the given class and his superclass. The Included_modules method lists all the modules that are included in this class.

Ruby Code

Class Parent
@ @var1 = nil
end
class Child < Parent
@ @var2 = nil
end
list1 = Parent.class_variables # ["@ @var1"]
list2 = array.included_modules # [enumerable, Kernel]  

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.