vim de oop
TRANSCRIPT
Vim Script De OOP
株式会社RAWHIDE.赤松 祐希(id:ukstudio)
自己紹介
宣伝
http://vim-users.jp
Lingrもあるよhttp://www.lingr.com/room/vim-users.jp
Vim Script書いたことない人?(́・ω・`)ノシ
:help script
Vimで書いてVimで実行してVimでデバッグしてVimで調べられる
ここからOOPの話
クラス定義
" クラス定義 let Person = s:klass.create({ \ '_class_': ['Person'], \ 'name': '', \ 'age' : 0})
ちょっとダサい・・・
メソッド定義
" メソッド定義 function! Person.hello() echo "Hello! My name is" self.name endfunction
let ukstudio = Person.new({'name': 'ukstudio', 'age': 22}) echo ukstudio call ukstudio.hello() “ Hello! My name is ukstudio
let tarou = Person.new({'name': 'tarou', 'age': 34}) call tarou.hello() “Hello! My name is tarou
メソッド一覧
"メソッドの一覧 echo ukstudio.methods() “ ['hello', 'extend', 'methods', 'is_a', 'new']
継承
"継承する let Member = Person.extend({'_class_':'Member'}) let member = Member.new({'name': 'member', 'age': 12})
call member.hello() “ Hello! My name is member
"継承関係にあるか echo ukstudio.is_a('Klass') “ 1 echo ukstudio.is_a('Person') “ 1 echo member.is_a('Person') “ 1
内部の話
http://gist.github.com/89661
サンプルも含めて全部ココ!
辞書
:help Dictionary
辞書の値に関数を渡せる
let di = {}
function Hello() echo ‘Hello’endfunction
let di.hello = function(‘Hello’)echo di.hello() “ Hello
let di = {}
function di.hello() dict echo ‘Hello’endfunction
echo di.hello()
JSのprototypeに似てる
ちょっとだけ仕組みの説明
" クラスの作成function! s:klass.create(...) dict let object = deepcopy(self) let class = copy(self._class_) for c in get(a:1, '_class_', []) call add(class, c) endfor let s:id_counter += 1 let object._id_ = s:id_counter call remove(object, 'create') call extend(object, a:1) let object._class_ = class return objectendfunction
" インスタンスの作成function! s:klass.new(...) dict let object = deepcopy(self) let s:id_counter += 1 let object._id_ = s:id_counter if a:0 >= 1 && !empty(a:1) call extend(object, a:1) endif return objectendfunction
let ukstudio = \ Person.new({'name': 'ukstudio', 'age': 22, 'hoge': 'hoge'}) echo ukstudio
{'hello': function('113'), 'age': 22, 'extend': function('112'), 'name': 'ukstudio', '_id_': 2, '_class_': ['Klass', 'Person'], 'methods': function('110'), 'hoge': 'hoge', 'is_a': function('111'), 'new': function('109')}
OOPの必要性
多分いらない
自分の発表全否定w
Rails.vimhttp://github.com/tpope/vim-rails/tree/master
スクリプトローカルな関数がたくさん
こっちの方がわかりやすい
それでもOOPがいいあなたへ
if_ruby
:help if_ruby
http://gist.github.com/89679
ruby << EOF class Person attr_accessor :name, :age
def initialize(name, age) @name = name @age = age end end
ukstudio = Person.new("ukstudio", 22) p ukstudio
require 'rubygems' require 'ruby-growl' g = Growl.new("localhost", "ruby-growl", ["ruby-growl Notification"]) g.notify("ruby-growl Notification", "Vim勉強会 in MTL", "発表中") EOF
ご静聴ありがとうございました