问题
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