How to use a Hash before defining it in Ruby -
I have a Ruby script that has a very long hash with more than 300 organizations. The script looks like this:
#! / Usr / bin / env ruby Array_A = [] myHash = {"x1" => "2", "x2" => "0", "x3" => "1",. . "X350" = & gt; "1"} myHash.keys.each do | Z | Array_A & lt; & Lt; "This key is" + z finally puts myHash.values.join ("|") Array_A.join ("|") But since hash is too large, the purpose of reading I will put a hash at the end of the script and puts each loop and command first, something like this: array_a = [] myHash .keys.each do | Z | Array_A & lt; & Lt; "This key is" + z end myHash.values.join ("|") puts Array_A.join ("|") myHash = {"x1" => "2", "x2" => "0", "x3" => "1",. . "X 350" = & gt; "1"} Is there any way to do this?
This is a little weird, but it is basically for data. The catch is that it is a file that contains the contents of the section after __ END __ , so you have to go to a hash with that. So something like this: array_a = [] myHash = eval DATA.read myHash.keys.each do | Z | Array_A & lt; & Lt; "This key is" + z finally puts myHash.values.join ("|") Array_A.join ("|") __END__ {"x1" => "2", "x2" => "0", "x3" => "1 one ?? | "X350" = & gt; "1"}
Comments
Post a Comment