-
-
Save anonymous/1371d9493d3860bbe086f96b9ea3e54f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'json' | |
data = JSON.load( | |
"{\"seqno\":2,\"attr\":{\"UID\":1000,\"ENVELOPE\":{\"date\":\"Mon, 1 Jan 2000 00:00:00 +0100\",\"subject\":\"Generic Subject\",\"from\":[{\"name\":\"=?utf-8?q?Generic FullName?=\",\"route\":{},\"mailbox\":\"genericmail\",\"host\":\"generichost.generictld\"}],\"sender\":[{\"name\":\"=?utf-8?q?Generic FullName?=\",\"route\":{},\"mailbox\":\"genericmail\",\"host\":\"generichost.generictld\"}],\"reply_to\":[{\"name\":\"=?utf-8?q?Generic FullName?=\",\"route\":{},\"mailbox\":\"genericmail\",\"host\":\"generichost.generictld\"}],\"to\":[{\"name\":{},\"route\":{},\"mailbox\":\"genericmailbox\",\"host\":\"generichost.generictld\"},{\"name\":{},\"route\":{},\"mailbox\":\"genericmailbox\",\"host\":\"generichost.generictld\"}],\"cc\":{},\"bcc\":{},\"in_reply_to\":{},\"message_id\":\"<GenericMessageID>\"}}}" | |
) | |
module EachWithParent | |
def each_with_parent(parent=nil, &blk) | |
case parent | |
when Hash, Struct, OpenStruct | |
each do |k, v| | |
v.each_with_parent(k, &blk) | |
end | |
when Array | |
map {|v| v.each_with_parent(k, &blk)} | |
else | |
blk.call(parent) | |
end | |
end | |
end | |
OpenStruct.send(:include, EachWithParent) | |
Struct.send(:include, EachWithParent) | |
Hash.send(:include, EachWithParent) | |
Array.send(:include, EachWithParent) | |
Object.send(:include, EachWithParent) | |
data.each_with_parent {|v| puts v.class } ## nil output |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment