如何从文件中读取文本到Ruby中的数组 [英] How to read text from file into an array in ruby

查看:109
本文介绍了如何从文件中读取文本到Ruby中的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法将net-ssh gem中的HOST,USER,PASS varibales当作一个数组来处理,并且有多个值?

我现在有一个带有HOST,USER和PASS值的外部文本文件,但是我需要将这些文件传递给net-ssh gem中的上述变量。



任何帮助和编辑澄清问题,非常感谢!
:)

解决方案

您应该可以使用splat运算符来执行此操作。请仔细阅读此内容,但Google很快会返回介绍。使用此方法,您可以将一组参数传递给您描述的方法。例如:

  some_values = ['host_name','user_name','password'] 
some_method(* some_values )

如果您可以更新您的问题以包含一些示例输入,和期望的结果,这将是非常方便进入更多的细节。



我不确定您的问题发生之前,在读取文件/值。如果是这样,给我看文件格式,我会想一想 - 你可能需要使用类似于 File.read('your_file.txt')



否则,请考虑将这些值存储在YAML文件中,这是用于在铁路应用程序之外存储敏感变量的常用模式。有了这个,你可以加载文件并以散列的形式访问数据,并作为参数传递。



这有帮助吗?希望如此 - 如果没有,请在问题中增加一些细节,我会更新以反映这一点。


Is there a way to treat the HOST, USER, PASS varibales in the net - ssh gem as an array and have multiple values for them?

I currently have an outside text file with the HOST, USER AND PASS values, but i need to pass these into the aforementioned variables within the net - ssh gem.

Any help and edits to clarify the question are appreciated! :)

解决方案

You should be able to do this using the splat operator. Have a read around on this, but Google quickly returns this intro. Using this, you can pass an array of arguments to a method as you describe. Something like:

some_values = ['host_name', 'user_name', 'password']
some_method(*some_values)

If you could update your question to include some example input, what you've tried so far, and the desired result, that'd be really handy to go into greater detail.

I'm not sure if your problem occurs before that, in reading the file / values. If so, show me the file format and I'll have a think - you'll likely need to use something like File.read('your_file.txt').

Otherwise, have a think on storing the values in a YAML file, a common pattern for storing sensitive variables outside of a rail app. With this, you can load the file and access the data as in a hash, and pass across as your arguments.

That help? Hope so - if not, add a little more detail to the question and I'll update to reflect this.

这篇关于如何从文件中读取文本到Ruby中的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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