* hash.c (hash_default_value): extract from rb_hash_aref(), to be
shared with rb_hash_shift(), so that overriding Hash#default will be respected. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@35197 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
15ca66efb0
commit
160d02d830
@ -1,3 +1,9 @@
|
|||||||
|
Sat Mar 31 14:22:59 2012 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||||
|
|
||||||
|
* hash.c (hash_default_value): extract from rb_hash_aref(), to be
|
||||||
|
shared with rb_hash_shift(), so that overriding Hash#default
|
||||||
|
will be respected.
|
||||||
|
|
||||||
Sat Mar 31 14:16:02 2012 Sokolov Yura (funny-falcon) <funny.falcon@gmail.com>
|
Sat Mar 31 14:16:02 2012 Sokolov Yura (funny-falcon) <funny.falcon@gmail.com>
|
||||||
|
|
||||||
* hash.c: do not allocate st_table when it is not necessary.
|
* hash.c: do not allocate st_table when it is not necessary.
|
||||||
|
29
hash.c
29
hash.c
@ -480,6 +480,20 @@ rb_hash_rehash(VALUE hash)
|
|||||||
return hash;
|
return hash;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static VALUE
|
||||||
|
hash_default_value(VALUE hash, VALUE key)
|
||||||
|
{
|
||||||
|
if (rb_method_basic_definition_p(CLASS_OF(hash), id_default)) {
|
||||||
|
VALUE ifnone = RHASH_IFNONE(hash);
|
||||||
|
if (!FL_TEST(hash, HASH_PROC_DEFAULT)) return ifnone;
|
||||||
|
if (key == Qundef) return Qnil;
|
||||||
|
return rb_funcall(ifnone, id_yield, 2, hash, key);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return rb_funcall(hash, id_default, 1, key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* call-seq:
|
* call-seq:
|
||||||
* hsh[key] -> value
|
* hsh[key] -> value
|
||||||
@ -500,13 +514,7 @@ rb_hash_aref(VALUE hash, VALUE key)
|
|||||||
st_data_t val;
|
st_data_t val;
|
||||||
|
|
||||||
if (!RHASH(hash)->ntbl || !st_lookup(RHASH(hash)->ntbl, key, &val)) {
|
if (!RHASH(hash)->ntbl || !st_lookup(RHASH(hash)->ntbl, key, &val)) {
|
||||||
if (!FL_TEST(hash, HASH_PROC_DEFAULT) &&
|
return hash_default_value(hash, key);
|
||||||
rb_method_basic_definition_p(CLASS_OF(hash), id_default)) {
|
|
||||||
return RHASH_IFNONE(hash);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
return rb_funcall(hash, id_default, 1, key);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return (VALUE)val;
|
return (VALUE)val;
|
||||||
}
|
}
|
||||||
@ -865,12 +873,7 @@ rb_hash_shift(VALUE hash)
|
|||||||
return rb_assoc_new(var.key, var.val);
|
return rb_assoc_new(var.key, var.val);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (FL_TEST(hash, HASH_PROC_DEFAULT)) {
|
return hash_default_value(hash, Qnil);
|
||||||
return rb_funcall(RHASH_IFNONE(hash), id_yield, 2, hash, Qnil);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
return RHASH_IFNONE(hash);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
|
@ -739,6 +739,14 @@ class TestHash < Test::Unit::TestCase
|
|||||||
h.each { assert_equal([1, 2], h.shift) }
|
h.each { assert_equal([1, 2], h.shift) }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_shift_none
|
||||||
|
h = Hash.new {|hh, k| "foo"}
|
||||||
|
def h.default(k = nil)
|
||||||
|
default_proc.call(k).upcase
|
||||||
|
end
|
||||||
|
assert_equal("FOO", h.shift)
|
||||||
|
end
|
||||||
|
|
||||||
def test_reject_bang2
|
def test_reject_bang2
|
||||||
assert_equal({1=>2}, {1=>2,3=>4}.reject! {|k, v| k + v == 7 })
|
assert_equal({1=>2}, {1=>2,3=>4}.reject! {|k, v| k + v == 7 })
|
||||||
assert_nil({1=>2,3=>4}.reject! {|k, v| k == 5 })
|
assert_nil({1=>2,3=>4}.reject! {|k, v| k == 5 })
|
||||||
|
Loading…
x
Reference in New Issue
Block a user