A Snippet for declaring ruby class in vim
I create a new file with vim, I want to define a class within it. In Ruby world, a lot of time I want to name my class as file_name.classify. So it will be nice to let vim generates this automatically for me.
I open up ruby.vim in ftplugin, a lot of snippets has already been defined there. Now I just need to add one more:
Snippet cla class ``ClassName()``<CR><{}><CR>end
What this does is when you enter “cla” and press <Tab> key (or whatever key you define for snippet expansion), it will call ClassName method and write down return value along with remaining string. Nothing fancy here.
Now let’s look at the most interesting part of this post: function ClassName()
function! ClassName() return substitute(expand("%:t:r"), '\(\%^\|_\)\(.\)', '\U\2', "g") endfunction
Basically what this function does is to convert underscored_word into UnderscoredWord.