Instantly share code, notes, and snippets.

Embed
What would you like to do?
Where is Waldo?
require 'active_support/all'
@store = ActiveSupport::HashWithIndifferentAccess.new
def my_hash
@store[:foo] ||= {bar: 'BAR'}
end
my_hash[:waldo] = 'WALDO'
my_hash[:baz] = 'BAZ'
my_hash[:qux] = 'QUX'
puts my_hash
@yemartin

This comment has been minimized.

Show comment
Hide comment
@yemartin

yemartin Sep 1, 2015

So... where is Waldo?

$ ruby where_is_waldo.rb
{"bar"=>"BAR", "baz"=>"BAZ", "qux"=>"QUX"}
Owner

yemartin commented Sep 1, 2015

So... where is Waldo?

$ ruby where_is_waldo.rb
{"bar"=>"BAR", "baz"=>"BAZ", "qux"=>"QUX"}
@coalwater

This comment has been minimized.

Show comment
Hide comment
@coalwater

coalwater Sep 1, 2015

The first assignment returns a normal hash, because @store[:foo] is nil, so it returns the normal hash {bar: 'BAR'}

I tested it with a small modification and it worked, still can't explain the exact reason though

def my_hash
  @store[:foo] ||= {bar: 'BAR'}.with_indifferent_access
end

# returns 
{"bar"=>"BAR", "waldo"=>"WALDO", "baz"=>"BAZ", "qux"=>"QUX"}

coalwater commented Sep 1, 2015

The first assignment returns a normal hash, because @store[:foo] is nil, so it returns the normal hash {bar: 'BAR'}

I tested it with a small modification and it worked, still can't explain the exact reason though

def my_hash
  @store[:foo] ||= {bar: 'BAR'}.with_indifferent_access
end

# returns 
{"bar"=>"BAR", "waldo"=>"WALDO", "baz"=>"BAZ", "qux"=>"QUX"}
@jcoleman

This comment has been minimized.

Show comment
Hide comment
@jcoleman

jcoleman Sep 1, 2015

@yemartin: What version of ActiveSupport? It works as expected for me in Rails console.

jcoleman commented Sep 1, 2015

@yemartin: What version of ActiveSupport? It works as expected for me in Rails console.

@yemartin

This comment has been minimized.

Show comment
Hide comment
@yemartin

yemartin Sep 2, 2015

@jcoleman I am seeing this with ActiveSupport 4.2.4.

Owner

yemartin commented Sep 2, 2015

@jcoleman I am seeing this with ActiveSupport 4.2.4.

@yemartin

This comment has been minimized.

Show comment
Hide comment
@yemartin

yemartin Sep 2, 2015

@coalwater Yes, that fixes it indeed. Here is another workaround that fixes the behavior too:

def my_hash
  @store[:foo] ||= {bar: 'BAR'}
  @store[:foo]
end

But the mystery remains... Why is the original code not working?

Owner

yemartin commented Sep 2, 2015

@coalwater Yes, that fixes it indeed. Here is another workaround that fixes the behavior too:

def my_hash
  @store[:foo] ||= {bar: 'BAR'}
  @store[:foo]
end

But the mystery remains... Why is the original code not working?

@yemartin

This comment has been minimized.

Show comment
Hide comment
@yemartin

yemartin Sep 2, 2015

@jcoleman Could it be related to the Ruby version? I am using 2.1.2 now, what about you?

Owner

yemartin commented Sep 2, 2015

@jcoleman Could it be related to the Ruby version? I am using 2.1.2 now, what about you?

@yemartin

This comment has been minimized.

Show comment
Hide comment
@yemartin

yemartin Sep 2, 2015

The answer was given by Matt Jones on the rubyonrails-core mailing list:

https://groups.google.com/d/msg/rubyonrails-core/SvRAIOu-hoU/4rXrXubUHQAJ

Owner

yemartin commented Sep 2, 2015

The answer was given by Matt Jones on the rubyonrails-core mailing list:

https://groups.google.com/d/msg/rubyonrails-core/SvRAIOu-hoU/4rXrXubUHQAJ

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment