是否有可能嵌入一个Ruby code到批处理文件? [英] Is it possible to embed a Ruby code into batch-file?

查看:190
本文介绍了是否有可能嵌入一个Ruby code到批处理文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常这是想在这个臭名昭著的例子

嵌入式code的好剧本不应该使用丑陋的转义序列,没有临时文件和冗余输出。
是否可以使用Ruby做什么?


解决方案

有一些hacks.Here的一个例子(文件应与的.bat 扩展名):

  @break#^
=开始关闭@echo
回声批次:世界,你好!
红宝石%〜F0%*
退出/ B 0=结束
把红宝石:世界,你好!

输出将是:


  

批次:你好!世界


  
  

RUBY:您好!世界


下面的解释。

有关红宝石 @break#^ 将宣布一个实例变量休息,将结束行以comment.On下一行会开始一个多行注释,其中一批code将被放置。

的Cmd.exe,另一方面将安静地执行break命令(因为如果@符号),因为break命令什么也不做(这是老DOS命令离开只是为了向后兼容),它不会有任何effect.The结束CARRET(它避开了在一批特殊符号)将难逃新线和前两行将会采取之一。该批次部分完成后,我们可以关闭红宝石评论,并把code。

这里可以看出,在批嵌code的几个例子(Python和PHP等)。

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?

解决方案

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

这篇关于是否有可能嵌入一个Ruby code到批处理文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆