わさっきhb

大学(教育研究)とか ,親馬鹿とか,和歌山とか,とか,とか.

続・Rubyのテンプレート

「foo-bar」から「FooBar」への変換は,Emacs Lispのマニュアルをもう少し調べて苦闘すれば,できそうにも思えますが,手間と利便性の兼ね合いから,今後の課題として残しておくこととします.

RubyとCのテンプレート - わさっき

残しておくと気持ち悪いので,少し調べたところ,Easy-Mmodeを使ってEmacsのマイナーモードを作る | feedforce Engineers' blog というのを見つけました.この中の,suite-dot-to-capitalize-stringとsuite-concat-capitalize-string-listが参考になりそうです.
Meadowのscratchモードで,ちょっと試してみましょう.

(defun suite-concat-capitalize-string-list (list)
  (cond
   ((= (length list) 1) (capitalize (car list)))
   (t (concat (capitalize (car list)) (suite-concat-capitalize-string-list (cdr list))))))

(defun suite-dot-to-capitalize-string (str)
  (suite-concat-capitalize-string-list (split-string str ".")))

のあとで,

(suite-dot-to-capitalize-string "takehiko")
""

(suite-dot-to-capitalize-string "takehiko.murakawa")
""

って,うまくいってないなあ….
split-stringが気になります.「"."」とあるけど,ここは正規表現なのではないかと.

(split-string "takehiko" ".")
("" "" "" "" "" "" "" "" "")

(split-string "takehiko" "\.")
("" "" "" "" "" "" "" "" "")

(split-string "takehiko" "\\.")
("takehiko")

(split-string "takehiko.murakawa" "\\.")
("takehiko" "murakawa")

思った通りです.\を2つ重ねるのは,"…"の中だからで,エスケープ文字ですね.
ともあれ,suite-concat-capitalize-string-listはそのままで,

(defun suite-dot-to-capitalize-string (str)
  (suite-concat-capitalize-string-list (split-string str "\\.")))

とすると,

(suite-dot-to-capitalize-string "takehiko")
"Takehiko"

(suite-dot-to-capitalize-string "takehiko.murakawa")
"TakehikoMurakawa"

となりました.
ちょっと待った…Rubyのファイル名ですから,区切りはdotではなくhyphenです.だからこうですか.

(defun suite-hyphen-to-capitalize-string (str)
  (suite-concat-capitalize-string-list (split-string str "-")))

(suite-hyphen-to-capitalize-string "takehiko")
"Takehiko"

(suite-hyphen-to-capitalize-string "takehiko-murakawa")
"TakehikoMurakawa"

(suite-hyphen-to-capitalize-string (file-name-sans-extension (file-name-nondirectory "/home/uchinoko/tmp/sankagetsu.rb")))
"Sankagetsu"

(suite-hyphen-to-capitalize-string (file-name-sans-extension (file-name-nondirectory "/home/uchinoko/tmp/watashi-baka-yone.rb")))
"WatashiBakaYone"

さて,~/.emacsの修正です.上のsuite-concat-capitalize-string-listとsuite-hyphen-to-capitalize-stringの定義を書き加え,先日は

(defvar template-replacements-alists
  '(("%rbclass%" . (lambda () (capitalize (file-name-sans-extension (file-name-nondirectory (buffer-file-name))))))))

としていたところを

(defvar template-replacements-alists
  '(("%rbclass%" . (lambda () (suite-hyphen-to-capitalize-string (file-name-sans-extension (file-name-nondirectory (buffer-file-name))))))))

に変更します.これでMeadowを起動して,watashi-baka-yone.rbというファイルを新規に開くと,クラス名は見事に,「WatashiBakaYone」となりました.