Skip to content

Instantly share code, notes, and snippets.

/file.rb Secret

Created May 25, 2016 22:36
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 anonymous/1371d9493d3860bbe086f96b9ea3e54f to your computer and use it in GitHub Desktop.
Save anonymous/1371d9493d3860bbe086f96b9ea3e54f to your computer and use it in GitHub Desktop.
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