Skip to content

Commit

Permalink
Merge pull request #10740 from mrsimo/hash-with-indifferent-access-se…
Browse files Browse the repository at this point in the history
…lect

HashWithIndifferentAccess#select working as intended
  • Loading branch information
rafaelfranca committed Jun 14, 2013
2 parents f711475 + 921ec9b commit 78234fe
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
Expand Up @@ -227,6 +227,10 @@ def symbolize_keys; to_hash.symbolize_keys! end
def deep_symbolize_keys; to_hash.deep_symbolize_keys end
def to_options!; self end

def select(*args, &block)
dup.select!(*args, &block)
end

# Convert to a regular hash with string keys.
def to_hash
_new_hash= {}
Expand Down
30 changes: 30 additions & 0 deletions activesupport/test/core_ext/hash_ext_test.rb
Expand Up @@ -480,6 +480,36 @@ def test_indifferent_deleting
assert_equal hash.delete('a'), nil
end

def test_indifferent_select
hash = ActiveSupport::HashWithIndifferentAccess.new(@strings).select {|k,v| v == 1}

assert_equal({ 'a' => 1 }, hash)
assert_instance_of ActiveSupport::HashWithIndifferentAccess, hash
end

def test_indifferent_select_bang
indifferent_strings = ActiveSupport::HashWithIndifferentAccess.new(@strings)
indifferent_strings.select! {|k,v| v == 1}

assert_equal({ 'a' => 1 }, indifferent_strings)
assert_instance_of ActiveSupport::HashWithIndifferentAccess, indifferent_strings
end

def test_indifferent_reject
hash = ActiveSupport::HashWithIndifferentAccess.new(@strings).reject {|k,v| v != 1}

assert_equal({ 'a' => 1 }, hash)
assert_instance_of ActiveSupport::HashWithIndifferentAccess, hash
end

def test_indifferent_reject_bang
indifferent_strings = ActiveSupport::HashWithIndifferentAccess.new(@strings)
indifferent_strings.reject! {|k,v| v != 1}

assert_equal({ 'a' => 1 }, indifferent_strings)
assert_instance_of ActiveSupport::HashWithIndifferentAccess, indifferent_strings
end

def test_indifferent_to_hash
# Should convert to a Hash with String keys.
assert_equal @strings, @mixed.with_indifferent_access.to_hash
Expand Down

0 comments on commit 78234fe

Please sign in to comment.