Example of Ruby code for getting the current class name

Source: Internet
Author: User

Ruby is currently the most flexible and agile programming language. It has special application methods and can bring a pleasant programming environment to programmers. Here we will learn the skills for Ruby to get the current class name.

  • Using pp () to output two-dimensional arrays in Ruby
  • Ruby on Rails directory files
  • Ruby function pointer concepts
  • Ruby Rmagick Installation Guide
  • Ruby special syntax concepts

Example code for Ruby to obtain the current Class Name:

  1. Module Kernel
  2. Private
  3. Def this_method_name
  4. Caller [0] = ~ /'([^'] *)
    '/And $1
  5. End
  6. End
  7. Class Foo
  8. Def test_method
  9. This_method_name
  10. End
  11. End
  12. Puts Foo. new. test_method
    # =>Test_method

Caller [0] = ~ /'([^'] *) '/And $1, later, I found that caller [0] [/'([^'] *) '/, 1] is also written, so I checked the usage of caller in the reference manual, a little clearer;

Caller Returns [file name, row number, method name]

The preceding section describes how to obtain the current class name in Ruby.

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.