0

I'm trying to figure out how to use define_method (or any other method) to generate array names as my method loops through an array and converts each hash element to JSON. Right now, my data is a series of hash arrays just dumped into a file, not contained in any larger array.

data_arrays = [{"key1":"value1"}, {"key2":"value2"}]
       [{"key3":"value3"}, ("key4":"value4")]
       [{"key5":"value5"}, ("key6":"value6")]

The code I was trying to work with is something like the following. Basically, I want each hash array to be converted into JSON and be assigned a new name, like json_array_1, jason_array_2, etc.

data_arrays.each do |element|
    for (i = 0; i < data_array.length; i+=1) do
        define_method("json_array_#{i}") do
            json_array_[i] = element.to_json
        end
    end
end

I'm aware this line: for (i = 0; i < data_array.length; i+=1) do is not really Ruby or good code at all. But I needed a way to iterate through the loop while also making the variable "i" available to be inserted in the json_array names. Can anyone give me any pointers for getting this code to work? I'm probably overthinking things and making this harder than it actually is.

2
  • You haven't presented your problem clear enough so I can only help with little things. (1) To get array index use data_arrays.each_with_index do |element, i| (2) To call a method dynamically use send("json_array_#{i}=", element.to_json) Commented Aug 11, 2016 at 18:45
  • Your definition of data_arrays has several errors. See how I defined it in my answer. Commented Aug 11, 2016 at 20:20

2 Answers 2

2
data_arrays.each_with_index do |element, i|
  define_method("json_array_#{i}") do
    element.to_json
  end
end

This will define methods json_array_0, json_array_1... Make it "json_array_#{i + 1}"if you want to start from `json_array_1.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the advice. However, this doesn't seem to be working at all for me. Nothing actually runs.
0
data_arrays = [[{key1:"value1"}, {key2:"value2"}],
               [{key3:"value3"}, {key4:"value4"}],
               [{key5:"value5"}, {key6:"value6"}]]
require 'json'

json_strings = data_arrays.map { |a| a.map { |h| h.to_json } }
  #=> [["{\"key1\":\"value1\"}", "{\"key2\":\"value2\"}"],
  #    ["{\"key3\":\"value3\"}", "{\"key4\":\"value4\"}"],
  #    ["{\"key5\":\"value5\"}", "{\"key6\":\"value6\"}"]]

or

json_strings = data_arrays.flat_map { |a| a.map { |h| h.to_json } }
  #=> ["{\"key1\":\"value1\"}", "{\"key2\":\"value2\"}", "{\"key3\":\"value3\"}", 
  #    "{\"key4\":\"value4\"}", "{\"key5\":\"value5\"}", "{\"key6\":\"value6\"}"] 

depending on your needs.

2 Comments

I see that you place data_arrays inside its own larger array. However, I'm working with a file that is generated as three separate hash arrays, no commas in between. When I try pushing the hash arrays into a larger array, I always keep ending up with strings. I'm not really sure why this is happening, although I agree it would be a lot easier if data_arrays was already contained with a larger array.
I don't understand. Are you saying those are text strings in a file? If so, please edit your answer to say so and put the strings in quotes. Also,it makes no sense to set data_arrays to something that is not a Ruby object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.