Ruby metaprogramming and ruby Programming

Source: Internet
Author: User

Ruby metaprogramming and ruby Programming

An object model

Kernel Module

Kernel. private_instance_methods.grep (/^ pr /)

 

Private method

1. If a method receiver is not yourself, you must specify a receiver.

2. A private method can only specify one implicit receiver (a private method that can be called as a superclass)

The Class itself is the object of the Class. The class name is a constant.

Class <Module, the Module and Class names act as Directory roles and are organized in a tree structure

When the include Module is used, M is inserted into the ancestor chain.

Method 2

Obj. send (: method, arg) # dynamically distribute ojb. method (arg)

Module # define_method # Replace def with a dynamic method

String # grep (){}

Def method_missing (method, * args)

Puts "you called: # {method} (# {args. join (',')}"

End

Sanblocks

Kernel # local_variables () trace the bound name

The ruby scope is not similar to the nested inside java/c #.

Class Definition, module definition, closing the previous scope when calling a method

Instance_eval

Instance_exec Parameter

Packaging code

Proc lambda

& Proc represents a code block

In proc, return indicates that the returned value is from the defined scope.

Lambda ruturn indicates that

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.