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.