Method for creating a method alias with the same name as the "keyword" in Ruby

Source: Internet
Author: User

Method for creating a method alias with the same name as the "keyword" in Ruby

Begin and end are ruby keywords, but Range is also known as begin and end instance methods. Now the question is: how to create their aliases?

If you use class Range; alias begin_x begin end, ruby thinks that the next begin is the beginning of a syntax block:

Copy codeThe Code is as follows:
2.1.2: 089> class Range; alias begin0 begin end
2.1.2: 090?>

The use of class Range; alias begin_x "begin" end does not work either. The use of alias begin_x: begin still does not work!

Copy codeThe Code is as follows:
2.1.2: 083> class Range
2.1.2: 084?> Alias begin0: begin
2.1.2: 085> end
2.1.2: 086?> End
SyntaxError: (irb): 86: syntax error, unexpected keyword_end, expecting end-of-input
From/Users/apple/. rvm/rubies/ruby-2.1.2/bin/irb: 11: in '<main>'
2.1.2: 087> class Range; alias begin0 "begin" end
SyntaxError: (irb): 87: syntax error, unexpected tSTRING_BEG
Class Range; alias begin0 "begin" end
^
From/Users/apple/. rvm/rubies/ruby-2.1.2/bin/irb: 11: in '<main>'

No way? I suddenly thought of this syntax: alias begin_x: "begin! The same end method is similar.

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.