Not a complete solution but step to the right direction. Should help you to go further...
irb(main):015:0> $stdout = File.open("console.log","w") #define output file
irb(main):016:0> query '*' #query all elements
irb(main):017:0> $stdout.flush # write output to the file
irb(main):018:0> $stdout = STDOUT # back to the original state
#<IO:<STDOUT>>
irb(main):019:0>