Ruby 输入图像文件
我正在使用 Ruby 编写编解码器来压缩图像。现在我正在使用硬编码的图像文件,但我想从用户那里检索图像文件。
收集这些信息的正确语法是什么?
这是我目前所拥有的:
# Set the path for the input image
path = "#{File.dirname(__FILE__)}/../../Images/my_image.png"
image = ImageList.new(path)
然后我使用 RMagick 操作图像,但这无关紧要。允许用户从控制台中当前路径向我提供图像的正确方法是什么?
谢谢!
I am using Ruby to write a codec to compress an image. Right now I am using a hardcoded image file, but I would like to retrieve an image file from the user.
What would be the proper syntax of gathering this information?
Here is what I currently have:
# Set the path for the input image
path = "#{File.dirname(__FILE__)}/../../Images/my_image.png"
image = ImageList.new(path)
Then I am manipulating the image using RMagick, but that's irrelevant. What is the proper way to allowing the user to give me an image from his current path in the console?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以像这样从命令行调用脚本:
$ myscript.rb path_to_file.jpg
在您的脚本中,您可以使用
ARGV
访问该字符串,例如You call your script from the commandline like this:
$ myscript.rb path_to_file.jpg
and in your script, you can access the string with
ARGV
, e.g.一种常见的方法是仅使用普通的旧 ARGV (请注意,与其他语言,这不包括程序名称)。如果您有强制参数,您可以执行以下操作:
您还可以使用各种选项解析器之一,例如 OptionParser 或 Trollop 如果你想要类似的东西
A common way would be to just use plain old ARGV (note that unlike other languages this does NOT include the program name). If you have mandatory arguments you can do something like:
You also could use one of the various option parsers like OptionParser or Trollop if you want something like