[ruby/yaml] Also use safe_load with each_value, values and shift

https://github.com/ruby/yaml/commit/f47d6123eb
This commit is contained in:
Hiroshi SHIBATA 2024-10-16 16:45:49 +09:00
parent d45fb19ee5
commit 1d7547f50d
2 changed files with 26 additions and 3 deletions

View File

@ -158,7 +158,7 @@ class DBM < ::DBM
#
# Returns +self+.
def each_value # :yields: value
super { |v| yield YAML.load( v ) }
super { |v| yield YAML.respond_to?(:safe_load) ? YAML.safe_load( v ) : YAML.load( v ) }
self
end
@ -167,7 +167,7 @@ class DBM < ::DBM
#
# Returns an array of values from the database.
def values
super.collect { |v| YAML.load( v ) }
super.collect { |v| YAML.respond_to?(:safe_load) ? YAML.safe_load( v ) : YAML.load( v ) }
end
# :call-seq:
@ -213,7 +213,9 @@ class DBM < ::DBM
# The order in which values are removed/returned is not guaranteed.
def shift
a = super
a[1] = YAML.load( a[1] ) if a
if a
a[1] = YAML.respond_to?(:safe_load) ? YAML.safe_load( a[1] ) : YAML.load( a[1] )
end
a
end

View File

@ -22,4 +22,25 @@ class TestYAMLDBM < Test::Unit::TestCase
assert_equal "value", @dbm.delete("key")
assert_nil @dbm["key"]
end
def test_each_value
@dbm["key1"] = "value1"
@dbm["key2"] = "value2"
@dbm.each_value do |value|
assert_match(/value[12]/, value)
end
end
def test_values
@dbm["key1"] = "value1"
@dbm["key2"] = "value2"
@dbm.values.each do |value|
assert_match(/value[12]/, value)
end
end
def test_shift
@dbm["key"] = "value"
assert_equal ["key", "value"], @dbm.shift
end
end