In Ruby 2.1.5 and 2.2.4, creating a new Collector returns the correct result.
require 'ostruct'
module ResourceResponses
class Collector < OpenStruct
def initialize
super
@table = Hash.new {|h,k| h[k] = Response.new }
end
end
class Response
attr_reader :publish_formats, :publish_block, :blocks, :block_order
def initialize
@publish_formats = []
@blocks = {}
@block_order = []
end
end
end
> Collector.new
=> #<ResourceResponses::Collector>
Collector.new.responses
=> #<ResourceResponses::Response:0x007fb3f409ae98 @block_order=[], @blocks= {}, @publish_formats=[]>
When I upgrade to Ruby 2.3.1, it starts returning back nil instead.
> Collector.new
=> #<ResourceResponses::Collector>
> Collector.new.responses
=> nil
I've done a lot of reading around how OpenStruct is now 10x faster in 2.3 but I'm not seeing what change was made that would break the relationship between Collector and Response. Any help is very appreciated. Rails is at version 4.2.7.1.
Let's have a look at the implementation of
method_missingin the current implementation:The interesting part is the block in the middle that runs when the method name didn't end with an
=and when there are no addition arguments:As you can see the implementation first checks if the key exists, before actually reading the value.
This breaks your implementation with the hash that returns a new
Response.newwhen a key/value is not set. Because just callingkey?doesn't trigger the setting of the default value:Ruby 2.2 didn't have this optimization. It just returned
@table[mid]without checking@table.key?first.