Difference between the dot (.) and double colon (: :) In method call

Source: Internet
Author: User

In Ruby, you can use periods (.) and double colons (: :) to call a method. The two methods are different only when the called method name starts with an uppercase letter.

When you call a method using the double colon (: :) method, if the first letter of the method name is in upper case and there is no parentheses during the call

1. If the caller is a class or module, the method will be mistakenly treated as a constant;

2. If the caller is a class instance, a syntax error is returned;

 

LabCodeAs follows:

 

Class cattr_accessor: constclassconst = 3def m1puts "hello" enddef C: M1 # class methods can be defined using the dot and double colon "C. m1 "enddef barputs" bar "enddef C: barputs" C. bar "endendc = C. NEWC. m1c: M1 # The class instance can call the class instance method puts "--------" C. m1 # class can call class method C: m1puts "--------" C. bar # C: bar # error, syntax error puts "--------" C. const = 1 puts "C. const = # {C. const} "# C: const = 2 # error, syntax error # puts" C. const = # {C: const} "# errorc: bar () # OKC. bar # OK # C: bar # error, uninitialized constant C: bar (nameerror) puts "C: classconst =#{ C: classconst }"

 

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.