Word for Windows Ruby Trick


(James) #1

This may be of limited value to many folks here, but I thought it kinda
neat.

I’m writing about Ruby, using Word for Windows, and find myself keeping a
GVim session running where I can test code before adding to the document.
I wondered if there was a way to get Word to evaluate Ruby text right from
the document itself.

I came up with a Word macro that uses gvim. There may be better ways,
perhaps sending it directly to the Ruby interpreter, but this approach has
the advantage that, after the code is run, it’s sitting in a GVim session
in case you want to edit it. (It requires you have gvim installed; you’ll
have to adjust the path to gvim.)

Sub EvalRuby()
’ Takes selection and runs it through ruby interpreter via Vim

’ First, make sure the selection is in the clipboard buffer …
Selection.Copy

’ Launch gvim, the graphical version of vim …
ret = Shell("F:\vim\vim60\gvim.exe ", vbNormalFocus)

’ Set focus to the launched app …
AppActivate ret

’ Paste the copied text. First, put Vim into insert mode:
SendKeys “{ESC} i”

’ Now insert the text with CTRL-V. It looks odd here because of the
’ SendKeys syntax for special control keys …
SendKeys “^(v)”

’ Save the file someplace in preparation for executing it …
SendKeys “{ESC}:w! c:\temp\word.rb {ENTER}”

’ Now tell Vim to execute the file using ruby
SendKeys “{ESC}:!ruby17 {%} {ENTER}”

End Sub

ruby17 is actually a DOS batch file I use to run ruby 1.7 (If i just call
’ruby’ it calls the PragProg 1.6 version.)

I’ve mapped this macro to a Word keyboard command, so now I can write some
code, select the text, and hit CTRL+SHIFT+R to see it run.

(I also believe there should be a way to use ActveRubyScript as a scripting
language in a VBA macro, but I haven’t pursued it. )

James


(Tobi Reif) #2

james@rubyxml.com wrote:

I’m writing about Ruby, using Word for Windows, and find myself keeping a
GVim session running where I can test code before adding to the document.
I wondered if there was a way to get Word to evaluate Ruby text right from
the document itself.

I’ve mapped this macro to a Word keyboard command, so now I can write some
code, select the text, and hit CTRL+SHIFT+R to see it run.

Neat.

I’m writing (about various stuff) in GVim (in XHTML, DocBook); I select
a portion of Ruby code, then type
:
after this,
’<,’>
is printed at the commandline; the I can send (write) this snippet to
Ruby, with
w !ruby

I call it with ;r

map ;r :w !ruby

This is probably old coffee for most RubyVimmers. (or “vimming Rubyists”?)

BTW, thanks for your getting started Vim info some weeks ago :slight_smile:

Tobi

···


http://www.pinkjuice.com/


(Tobi Reif) #3

james@rubyxml.com wrote:

’ Paste the copied text. First, put Vim into insert mode:
SendKeys “{ESC} i”

’ Now insert the text with CTRL-V. It looks odd here because of the
’ SendKeys syntax for special control keys …
SendKeys “^(v)”

’ Save the file someplace in preparation for executing it …
SendKeys “{ESC}:w! c:\temp\word.rb {ENTER}”

’ Now tell Vim to execute the file using ruby
SendKeys “{ESC}:!ruby17 {%} {ENTER}”

perhaps simply send
"+PggVG:w !ruby
; no tempfile involved.

Tobi

···


http://www.pinkjuice.com/


(Tobi Reif) #4

Hi,

Selecting a portion of Ruby code (visual mode),
sending it to Ruby with ;r

vmap ;r :w !rubygv

works for sending a string to Ruby, but unfortunately it sends the whole
line?

How to send only the selected part of the line?

It sems to be stored in @" after y.

For -e solutions, "s in the “” the string will have to be escaped.
perhaps something like


let string = y
[escape "s in the string]
:!ruby -e ‘string’

Tobi

···


http://www.pinkjuice.com/


(James) #5

Neat.

I’m writing (about various stuff) in GVim (in XHTML, DocBook); I select
a portion of Ruby code, then type
:
after this,
’<,’>
is printed at the commandline; the I can send (write) this snippet to
Ruby, with
w !ruby

I call it with ;r

map ;r :w !ruby

I use some Ruby menu items that map a few function keys to Ruby calls
(executing text, commenting/uncomment).
I have it set up so that F5 runs ruby 1.6 and F7 runs 1.7; makes it easier
to see difference in behavior.

This is probably old coffee for most RubyVimmers. (or “vimming
Rubyists”?)

See: http://www.rubygarden.org/ruby?VimExtensions

BTW, thanks for your getting started Vim info some weeks ago :slight_smile:

Oh, my pleasure. And thanks to the other Vim-Rubyites out there.

James

···

Tobi


http://www.pinkjuice.com/


(James) #6

’ Now tell Vim to execute the file using ruby
SendKeys “{ESC}:!ruby17 {%} {ENTER}”

perhaps simply send
"+PggVG:w !ruby
; no tempfile involved.

I’ll have to play with that one. I get an error, so SendKeys is either
sending something extraneous, or I’ve omitted/added an ESC or something …

But it seems faster, at least until the error.

I’m also thinking about how to execute the code in the Word doc, and have
the macro insert the results right under the code.

Of course, I have to get some actual work done, too …

James

···

Tobi


http://www.pinkjuice.com/


(Tobi Reif) #7

james@rubyxml.com wrote:

I’ll have to play with that one. I get an error, so SendKeys is either
sending something extraneous, or I’ve omitted/added an ESC or something …

perhaps try
"+P:%w !ruby

You’ll have to somehow escape the ".

Tobi

···


http://www.pinkjuice.com/


(Tobi Reif) #8

james@rubyxml.com wrote:

I use some Ruby menu items that map a few function keys to Ruby calls
(executing text

Can you select only a portion of a line and have it executed? (instead
of whole lines) Can you send a multiline selection to Ruby, without
sending all involved lines a whole lines? If so, perhaps you could post
the macros, or pointers to them.

Tobi

···


http://www.pinkjuice.com/