From: mike lupo Date: 2012-02-25T10:56:23+09:00 Subject: Re: How do I recursively traverse a data structure containing a mix of types? Thanks Ryan Davis. Problem #1, not every piece of data passed in is a hash. So restructuring the function a little helps clarify that. Apologies for my shoddy first attempt, and thanks again you for your input. For the readers finding this via search, find the input contained in a text file attached to my initial post. #2, I have this working now. Not elegant, hardly fault tolerant, but works. #define a boolean "test". class Object def boolean? self.is_a?(TrueClass) || self.is_a?(FalseClass) end end def traverse_data(input, indent="") puts "#{indent}traverse_data -- >" if input.class==Fixnum puts "#{indent}Integer: #{input}" return end if input.boolean? puts "#{indent}Boolean: #{input}" return end if input.class==Bignum puts "#{indent}Bignum: #{input}" return end input.each do |v| case v when String then puts "#{indent}STRING #{v}" puts "#{indent}<--traverse_data" return when Array then v.each_index {|i| #puts traverse_data(v[i], "#{indent} ")} when Hash then puts "#{indent}HASH: #{v}" traverse_data(v, "#{indent} ") #return v else raise ArgumentError, "Unhandled type #{v.class}" end end end #call it now. input = {gotten from attached text file} traverse_data(input) -- Posted via http://www.ruby-forum.com/.