begin和end是ruby的關鍵字,但是range中也有名稱為begin和end的執行個體方法。現在問題來了:怎麼建立它們的别名方法?
如果用class range;alias begin_x begin end是不行的,ruby以為後面一個begin是某個文法塊的開頭啊:
2.1.2 :089 > class range;alias begin0 begin end
2.1.2 :090?>
用class range;alias begin_x "begin" end也不行,用alias begin_x :begin還是不行!
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
^
沒法子了嗎?我突然想到了這個文法:alias begin_x :"begin",這回哦鳥!同樣的end方法類似。