Is it possible to embed a Ruby code into batch-fil

2019-08-12 07:19发布

Usually this is useful for “self-calling” scripts like in this notorious example

The good script with embedded code should not use ugly escape sequences , no temp files and redundant output. Is it possible to be done with Ruby?

2条回答
相关推荐>>
2楼-- · 2019-08-12 07:44

Yes with some hacks.Here’s an example ( file should be with .bat extension ):

@break #^
=begin

@echo off
echo BATCH: Hello world!
ruby "%~f0" %*
exit /b 0

=end
puts 'RUBY: Hello world!'

Output will be:

BATCH: Hello world!

RUBY: Hello world!

Here’s the explanation.

For Ruby @break #^ will declare an instance variable break and will end the line with a comment.On the next line it will start a multi line comment where the batch code will be placed.

Cmd.exe on the other hand will execute silently the break command (because if the @ symbol) and because break command do nothing (it is and old dos command left only for backward compatibility) it will have no effect.The ending carret (it escapes the special symbols in batch) will escape the new line and first two lines will be taken for one. After the batch part is done we can close the Ruby comment and put the code.

Here can be seen few more examples of embedded code in batch (Python,PHP and so on).

查看更多
Luminary・发光体
3楼-- · 2019-08-12 07:59

This is another hack, IMHO a bit easier to understand as it does not depend on line continuation, block level comments and a CMD statement which is there only for backwards compatibility.

The main thing is, that the bat file also must be a syntactically valid ruby file. Within this ruby-file we need to embed the CMD syntax. For this we need to create an island of code which is transparent to ruby:

  1. @rem creates a ruby variable @rem and is occasionally the comment statement in bat. to the line is ignore, but allows to open an island of bat code in the ruby file.
  2. exit /b 0 terminates the part parsed by command.com

.

@rem = %Q{

@echo off
echo BATCH: Hello world!
ruby "%~f0" %*
exit /b %ERRORLEVEL%

}

# ruby code starts here

puts %Q{RUBY: Hello world! #{ARGV}}
查看更多
登录 后发表回答