Is it possible to embed a Ruby code into batch-file?

时光总嘲笑我的痴心妄想 提交于 2020-02-05 23:32:34

问题


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?


回答1:


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).




回答2:


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}}


来源:https://stackoverflow.com/questions/35094778/is-it-possible-to-embed-a-ruby-code-into-batch-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!