Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Hash#select and #reject on ruby 2.1.1 #14198

Merged
merged 4 commits into from
Mar 8, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,14 @@ 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.tap { |hash| hash.select!(*args, &block) }
end

def reject(*args, &block)
dup.tap { |hash| hash.reject!(*args, &block) }
end

# Convert to a regular hash with string keys.
def to_hash
_new_hash= {}
Expand Down
8 changes: 8 additions & 0 deletions activesupport/lib/active_support/ordered_hash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ def encode_with(coder)
coder.represent_seq '!omap', map { |k,v| { k => v } }
end

def select(*args, &block)
dup.tap { |hash| hash.select!(*args, &block) }
end

def reject(*args, &block)
dup.tap { |hash| hash.reject!(*args, &block) }
end

def nested_under_indifferent_access
self
end
Expand Down
30 changes: 30 additions & 0 deletions activesupport/test/core_ext/hash_ext_test.rb
Original file line number Diff line number Diff line change
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 { |_ ,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! { |_, 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 { |_, 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! { |_, 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
5 changes: 4 additions & 1 deletion activesupport/test/ordered_hash_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,9 @@ def test_find_all
end

def test_select
assert_equal @keys, @ordered_hash.select { true }.map(&:first)
new_ordered_hash = @ordered_hash.select { true }
assert_equal @keys, new_ordered_hash.map(&:first)
assert_instance_of ActiveSupport::OrderedHash, new_ordered_hash
end

def test_delete_if
Expand All @@ -143,6 +145,7 @@ def test_reject
assert_equal copy, @ordered_hash
assert !new_ordered_hash.keys.include?('pink')
assert @ordered_hash.keys.include?('pink')
assert_instance_of ActiveSupport::OrderedHash, new_ordered_hash
end

def test_clear
Expand Down