How to read text from file into an array in ruby

99封情书 提交于 2019-12-11 16:56:14

问题


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! :)


回答1:


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.



来源:https://stackoverflow.com/questions/47870588/how-to-read-text-from-file-into-an-array-in-ruby

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