Vim, recent if_ruby changes, and TwitVim

Feb 27, 2010 13:40


Bram recently added a bunch of if_ruby (Ruby Interface) patches to Vim to add support for Ruby 1.9, improve overall support for Ruby, and fix a few problems. Of course, two of those patches affected TwitVim and I'll discuss them here. First, the good stuff: Patch 7.2.360 finally fixes the if_ruby Windows sockets problem. I used to recommend this unofficial patch but that won't be needed any more if the user has patch 7.2.360 or (the upcoming) Vim 7.3 or later.

Patch 7.2.374 allows VIM::evaluate() to return Ruby arrays or hashes instead of strings if the Vim expression results in a list or dictionary. This, unfortunately, did break TwitVim because TwitVim does the following:

keys = VIM.evaluate('keys(a:parms)')
keys = keys.split(/\n/)
If the parms dictionary has keys "user", "text", and "status", prior to patch 7.2.374, VIM.evaluate() will return "user\ntext\nstatus" and the next line will split it into the array ["user", "text", "status"]. After patch 7.2.374, VIM.evaluate() returns ["user", "text", "status"], causing the next line to hit a Ruby error because a Ruby array doesn't have a split() method.

How do we solve this problem? At first, I was thinking of checking for this Vim patch using a conditional like this:

v:version > 702 || v:version == 702 && has("patch374")
In other words, Vim 7.3 and later are guaranteed to have patch 7.2.374 and we only check for the patch itself if the Vim version is 7.2. If I stick this expression into VIM.evaluate(), it'll return "1" if that patch is present and "0" otherwise. Then it occurred to me that there are two better ways to deal with this:

1. Make the Vim expression return a string regardless by doing an explicit join on the array:

keys = VIM.evaluate('join(keys(a:parms), "\n")')
keys = keys.split(/\n/)
2. Use Ruby's type introspection to see if a split is needed:

keys = VIM.evaluate('keys(a:parms)')
keys = keys.split(/\n/) if keys.is_a? String
I prefer the second approach because it is more efficient and a few years into the future, once everyone has upgraded their Vim installations past this patch, we can clean up the code easily by removing the second line. Thus, this fix is in TwitVim revision 82.

if_ruby, vim, ruby, twitvim

Previous post Next post
Up