, ; . .
def hash_to_eval_chain(hsh)
make_eval_chain(hsh).join "."
end
private
def make_eval_chain(obj)
if obj.is_a? Hash
raise "Hash cannot contain multiple key-value pairs unless they are nested" if obj.size > 1
return make_eval_chain(obj.to_a.flatten)
elsif obj.is_a? Array
return [ obj.first, *make_eval_chain(obj.last) ] if obj.last.is_a? Hash
return obj if [String, Symbol, Array].include? obj.class
raise "Only strings, symbols, and hashes are allowed as values in the hash."
else
raise "Expected Hash, received #{obj.class}"
end
end
, , .
, {:one => {:two => :three}}.to_a.flatten [:one, {:two => :three}] A-ha!, car/cdr.
:
, , . ( Facets # ):
def hash_to_eval_chain(hsh)
make_eval_chain(hsh).flatten.join "."
end
private
def make_eval_chain(obj)
if obj.is_a? Hash
return obj.map {|key, val| [key, *make_eval_chain(val)] } if obj.is_a?(Hash) && obj.size <= 1
raise "Hash cannot contain multiple key-value pairs unless they are nested"
else
return obj if [String, Symbol, Array].any? { |klass| obj.is_a? klass }
raise "Only strings, symbols, and hashes are allowed as values in the Hash."
end
end