Nếu bạn kiểm soát nội dung của băm, và nó là tương đối nhỏ, tôi sẽ sử dụng một cái gì đó như thế này trong một phương pháp riêng như một giải pháp chung chung.
def exclusively_true?(hash, key)
return false unless hash.delete(key) == true
!hash.has_value? true
end
require 'test/unit'
class TestExclusive < Test::Unit::TestCase
def setup
@test_hash = {foo: true, bar: false, hoge: false}
end
def test_exclusive
assert_equal(true, exclusively_true?(@test_hash, :foo))
end
def test_inexclusive
@test_hash[:bar] = true
assert_equal(false, exclusively_true?(@test_hash, :foo))
end
end
require 'benchmark'
h = {foo: true}
999.times {|i| h["a#{i}"] = false}
Benchmark.bmbm(30) do |x|
x.report('exclusively_true') do
1000.times do
exclusively_true?(h, :foo)
end
end
end
chuẩn giả tạo: (OS X 10.8.3/3 GHz/8 GB)
ruby -v: ruby 2.0.0p0 (2013-02-24 revision 39474) [x86_64-darwin12.3.0]
Rehearsal ------------------------------------------------------------------
exclusively_true 0.000000 0.000000 0.000000 ( 0.000412)
--------------------------------------------------------- total: 0.000000sec
user system total real
exclusively_true 0.000000 0.000000 0.000000 ( 0.000331)
như thế nó không phá hủy băm –
Đôi khi nó chỉ giúp suy nghĩ một cách hợp lý những gì bạn đang cố gắng để thực hiện. Cách tiếp cận tuyệt vời +1. – squiguy