summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--mrbgems/mruby-hash-ext/mrblib/hash.rb20
-rw-r--r--mrbgems/mruby-hash-ext/test/hash.rb9
-rw-r--r--src/vm.c1
-rw-r--r--test/t/proc.rb17
-rw-r--r--test/t/range.rb14
-rw-r--r--test/t/string.rb10
6 files changed, 66 insertions, 5 deletions
diff --git a/mrbgems/mruby-hash-ext/mrblib/hash.rb b/mrbgems/mruby-hash-ext/mrblib/hash.rb
index a5f04e5e1..259b0fa12 100644
--- a/mrbgems/mruby-hash-ext/mrblib/hash.rb
+++ b/mrbgems/mruby-hash-ext/mrblib/hash.rb
@@ -160,4 +160,24 @@ class Hash
end
self
end
+
+ ##
+ # call-seq:
+ # hsh.key(value) -> key
+ #
+ # Returns the key of an occurrence of a given value. If the value is
+ # not found, returns <code>nil</code>.
+ #
+ # h = { "a" => 100, "b" => 200, "c" => 300, "d" => 300 }
+ # h.key(200) #=> "b"
+ # h.key(300) #=> "c"
+ # h.key(999) #=> nil
+ #
+
+ def key(val)
+ self.each do |k, v|
+ return k if v == val
+ end
+ nil
+ end
end
diff --git a/mrbgems/mruby-hash-ext/test/hash.rb b/mrbgems/mruby-hash-ext/test/hash.rb
index 1334522ff..b9992fa96 100644
--- a/mrbgems/mruby-hash-ext/test/hash.rb
+++ b/mrbgems/mruby-hash-ext/test/hash.rb
@@ -102,3 +102,12 @@ assert("Hash#keep_if") do
h = { 1 => 2, 3 => 4, 5 => 6 }
assert_equal({ 1 => 2, 3=> 4, 5 =>6} , h.keep_if { true })
end
+
+assert("Hash#key") do
+ h = { "a" => 100, "b" => 200, "c" => 300, "d" => 300, nil => 'nil', 'nil' => nil }
+ assert_equal "b", h.key(200)
+ assert_equal "c", h.key(300)
+ assert_nil h.key(999)
+ assert_nil h.key('nil')
+ assert_equal 'nil', h.key(nil)
+end
diff --git a/src/vm.c b/src/vm.c
index 317b5f6b1..620f9e977 100644
--- a/src/vm.c
+++ b/src/vm.c
@@ -1307,6 +1307,7 @@ RETRY_TRY_BLOCK:
mrb->c->ci->argc = len;
if (argc < len) {
regs[len+1] = *blk; /* move block */
+ SET_NIL_VALUE(regs[argc+1]);
if (argv0 != argv) {
value_move(&regs[1], argv, argc-m2); /* m1 + o */
}
diff --git a/test/t/proc.rb b/test/t/proc.rb
index 151e1df86..1be73c99a 100644
--- a/test/t/proc.rb
+++ b/test/t/proc.rb
@@ -55,6 +55,23 @@ assert('Proc#call', '15.2.17.4.3') do
assert_equal 5, a2
end
+assert('Proc#call proc args pos block') do
+ pr = proc {|a,b,&c|
+ [a, b, c.class, c&&c.call(:x)]
+ }
+ assert_equal [nil, nil, Proc, :proc], (pr.call(){ :proc })
+ assert_equal [1, nil, Proc, :proc], (pr.call(1){ :proc })
+ assert_equal [1, 2, Proc, :proc], (pr.call(1, 2){ :proc })
+ assert_equal [1, 2, Proc, :proc], (pr.call(1, 2, 3){ :proc })
+ assert_equal [1, 2, Proc, :proc], (pr.call(1, 2, 3, 4){ :proc })
+
+ assert_equal [nil, nil, Proc, :x], (pr.call(){|x| x})
+ assert_equal [1, nil, Proc, :x], (pr.call(1){|x| x})
+ assert_equal [1, 2, Proc, :x], (pr.call(1, 2){|x| x})
+ assert_equal [1, 2, Proc, :x], (pr.call(1, 2, 3){|x| x})
+ assert_equal [1, 2, Proc, :x], (pr.call(1, 2, 3, 4){|x| x})
+end
+
assert('Proc#return_does_not_break_self') do
class TestClass
attr_accessor :block
diff --git a/test/t/range.rb b/test/t/range.rb
index deaa3997f..b35da40ab 100644
--- a/test/t/range.rb
+++ b/test/t/range.rb
@@ -74,6 +74,20 @@ assert('Range#member?', '15.2.14.4.11') do
assert_false a.member?(20)
end
+assert('Range#to_s', '15.2.14.4.12') do
+ assert_equal "0..1", (0..1).to_s
+ assert_equal "0...1", (0...1).to_s
+ assert_equal "a..b", ("a".."b").to_s
+ assert_equal "a...b", ("a"..."b").to_s
+end
+
+assert('Range#inspect', '15.2.14.4.13') do
+ assert_equal "0..1", (0..1).inspect
+ assert_equal "0...1", (0...1).inspect
+ assert_equal "\"a\"..\"b\"", ("a".."b").inspect
+ assert_equal "\"a\"...\"b\"", ("a"..."b").inspect
+end
+
assert('Range#eql?', '15.2.14.4.14') do
assert_true (1..10).eql? (1..10)
assert_false (1..10).eql? (1..100)
diff --git a/test/t/string.rb b/test/t/string.rb
index 04f90fb45..a2a020a79 100644
--- a/test/t/string.rb
+++ b/test/t/string.rb
@@ -475,6 +475,11 @@ assert('String#upcase!', '15.2.10.5.43') do
assert_equal 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', b
end
+assert('String#inspect', '15.2.10.5.46') do
+ ("\1" * 100).inspect # should not raise an exception - regress #1210
+ assert_equal "\"\\000\"", "\0".inspect
+end
+
# Not ISO specified
assert('String interpolation (mrb_str_concat for shared strings)') do
@@ -506,8 +511,3 @@ assert('String#each_byte') do
assert_equal bytes1, bytes2
end
-
-assert('String#inspect') do
- ("\1" * 100).inspect # should not raise an exception - regress #1210
- assert_equal "\"\\000\"", "\0".inspect
-end