Skip to content

Instantly share code, notes, and snippets.

@hololeap
Last active September 27, 2015 23:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hololeap/30c501e46629c7529d81 to your computer and use it in GitHub Desktop.
Save hololeap/30c501e46629c7529d81 to your computer and use it in GitHub Desktop.
TypeError: no implicit conversion of Array into String
from (irb):19:in `new'
from (irb):19
from /usr/bin/irb:11:in `<main>'
class SubarrayStruct < Struct
# Takes an Array of variables that will be initialized as empty arrays, and a list of normal variables
def initialize(subarray_names, *variables)
# Turn sublist_names into an Array (if not already) and combine it with variables, send
# this new Array to Struct.new as a list of arguments
super *(variables + Array(subarray_names)) do
define_method :initialize do
# Initializes each variable in sublist_names with an empty array
Array(subarray_names).each { |s| send "#{s}=", [] }
end
end
end
end
Test = SubarrayStruct.new [:array_one, :array_two], :var_three, :var_four
Test.new
variables = [:var_three, :var_four]
subarray_names = [:array_one, :array_two]
Test = Struct.new *(variables + Array(subarray_names)) do
define_method :initialize do
Array(subarray_names).each { |s| send "#{s}=", [] }
end
end
Test.new
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment