Skip to content

subclassing doesn't work #1

@chuckremes

Description

@chuckremes

See this gist for the same error under both rubinius and mri 1.9.2:

https://gist.github.com/1079682

Here is my use-case. I send hashes over the network to communicate amongst distributed processes. The hash oftentimes look like so:

message = {
'code' => 3,
'operation' => 'create',
'payload' => {
'field1' => [1,2,3],
'field2' => 'string',
'field3' => 5
}
}

I would like to be able to pass that entire hash to a ClassyStruct and conditionally have only the payload get parsed. When accessing 'field1' and others, I don't want to do foo.payload.field1 (I throw away 'code' and 'operation'). I want to reach them directly like foo.field1.

So I write code like:

MessageParent = ClassyStruct.new

class Message < MessageParent
def initialize(document = nil)
if document
if document.has_key?('payload')
super(document['payload'])
else
super(document)
end
else
super()
end
end
end

This allows me to pass a one-level hash to Message and have it parsed the way I like it. The initializer also let's me pass a hash with an embedded payload and parse it the way I like it. Lastly, I can pass nothing/nil and set the fields manually.

Unfortunately, subclassing a ClassyStruct blows up. :(

Is there a work around or a fix?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions