Invoking Ruby.exe on windows

If you look at c:\Ruby\bin you will find two sets of files. One with .bat extension and one without.
for example let’s take a look at those two

c:\Ruby\bin\rake and rake.bat

rake (without extension) looks like this:

#!C:/RUBY/bin/ruby.exe
#
# This file was generated by RubyGems.
#
# The application 'rake' is installed as part of a gem, and
# this file is here to facilitate running it.
#

require 'rubygems'

version = ">= 0"

if ARGV.first =~ /^_(.*)_$/ and Gem::Version.correct? $1 then
  version = $1
  ARGV.shift
end

gem 'rake', version
load 'rake'

and rake.bat looks like this

@ECHO OFF
IF NOT "%~f0" == "~f0" GOTO :WinNT
@"ruby.exe" "C:/RUBY/bin/rake" %1 %2 %3 %4 %5 %6 %7 %8 %9
GOTO :EOF
:WinNT
@"ruby.exe" "%~dpn0" %*

so what happens when you run rake from the command line? it uses the first line in a file #!C:/RUBY/bin/ruby.exe -ws as a directive to run this file using ruby.exe the -ws is for warnings and swtich parsing. Look for yourself when you run ruby -help and read the help on switches.

Everything else in that rake files is pure ruby code

The interesting file is the rake.bat. The %1 to %9 are arguments. %0 being the name of the executing file (not always) but in ruby similar to $0.

The “%~dpn0” in last line I think stands for “d(rive)p(ath)n(ame)” but 0 ? maybe something with extension. I don’t really know. I am rusty witn bat arguments processing.

and the last %* bit just collects the rest of arguments. You can make a temp.bat file and put this line in it.

REM "%~d0", "%~p0", "%~f0", "%~n"

this should be a fun exercise.

So. If you have ruby files that you want to execute from the command then put a windows path to ruby directive as the first line

#!C:/RUBY/bin/ruby.exe

and save the file without any extension to a dir that’s in your path (I use ~/bin or in windows speak USERPROFILE\bin )

blog comments powered by Disqus
rss icon