Borrowing From Ruby Tapas
Author: John on November 04, 2014
I have many learning tools in my arsenal including Code School, Thoughtbot/Upcase, RailsCasts, Ruby Tapas, and too many other Internet based free resources to list. But today I want to talk tapas, and borrow something that I learned from watching many episodes of Ruby Tapas.
As I've watched various episodes, I kept saying to myself, how is Avdi executing code right from within his text editor? Is this some time of post production trickery? Perhaps a green screen based laptop with a dash of After Effects? But after some research, I learned that is wasn't some fancy magic, but rather an ability within his editor. I believe that editor to be Emacs.
Well, I'm a Sublime Text user, and slowly dabbling with Vim. So off to the online oracles to see if I could find a solution that would work in Sublime. I did indeed find some solutions that said they were doing what Avdi can do - even referencing that they had the same desire as me. However, I was not successful with getting Sublime to work the way I was expecting. After spending several hours trying to force it to work, I gave up. And I had a thought - surely this would be easier to get working with Vim. :)
Getting the Tapas style in-line Ruby code execution to work in Vim was nice and easy. I followed the instructions here and was up in running with very little effort. For those that don't want leave from this fabulous reading, I'll summarize what needs to be done, first starting with the installation of a gem:
gem install rcodetools
Now we need to add a plugin to Vim. Depending on your setup, you may need to add the .vim/bundle directory in your home directory. But I already had this setup, so all I needed to do was the following:
cd ~ cd .vim/bundle git clone https://github.com/t9md/vim-ruby-xmpfilter.git
For a final bit of setup/configuration, we need to take a trip in editing the ~/.vimrc or ~/.vimrc.local file so we can do some mapping:
nmap <buffer> <F4> <Plug>(xmpfilter-mark) xmap <buffer> <F4> <Plug>(xmpfilter-mark) imap <buffer> <F4> <Plug>(xmpfilter-mark) nmap <buffer> <F5> <Plug>(xmpfilter-run) xmap <buffer> <F5> <Plug>(xmpfilter-run) imap <buffer> <F5> <Plug>(xmpfilter-run)
The above mapping will allows us to mark (F4) what line(s) we want to see the results for of executing the ruby code, which we accomplish by pressing F5. Let's step through an example to see how this works. I'll show the example file through the various stages.
1 class Person 2 attr_accessor :name, :age 3 4 def initialize(name) 5 self.name = name 6 self.age = "Does it matter?" 7 end< 8 end 9 10 p = Person.new('Kevin') 11 p.name 12 p.age
Next we will press F4 for the code we want to evaluate, line 11 below:
1 class Person 2 attr_accessor :name, :age 3 4 def initialize(name) 5 self.name = name 6 self.age = "Does it matter?" 7 end 8 end 9 10 p = Person.new('Kevin') 11 p.name # => 12 p.age
And finally, EXECUTE (F5):
1 class Person 2 attr_accessor :name, :age 3 4 def initialize(name) 5 self.name = name 6 self.age = "Does it matter?" 7 end 8 end 9 10 p = Person.new('Kevin') 11 p.name # => "Kevin" 12 p.age
Pretty neat, eh? Borrowing this workflow from Ruby Tapas will now help me when I'm following along with each episode. Now I just need to get better with Vim.
Learn Something New Every Day
Last Edited by: John on November 11, 2015