Skip to content
This repository
Browse code

fix named argument

Test Script:
----
require 'test/unit/assertions.rb'
include Test::Unit::Assertions

assert_equal(
  "foo, bar",
  "%{firstname}, %{lastname}" % {firstname: "foo", lastname: "bar"}
)

assert_equal(
  "10, 43.5",
  "%<integer>d, %<float>.1f" % { integer: 10, float: 43.476374 }
)

assert_equal(
  "10<float>",
  "%{integer}<float>" % { integer: 10, float: 43.476374 }
)

assert_raise(ArgumentError){ "%<foo><bar>" % { foo: 10, bar: 42 } }
#----
assert_equal(
  "01 : 02",
  "%<foo>02d : %<bar>02d" % { :foo => 1, :bar => 2 }
)

assert_equal(
  "01<foo>",
  "%02d<foo>" % [1, {:foo => 1}]
)
assert_raise(ArgumentError){ "%s %1$s" % "foo" }
assert_raise(ArgumentError){ "%02d%<foo>" % [1, {:foo => 1}] }
assert_raise(ArgumentError){ "%1$<key2>s" % {:key => "value"} }
  • Loading branch information...
commit 53b78caf0339d8544d0970b2fc58d6896e8ad57f 1 parent 2982681
Watson authored February 01, 2012

Showing 1 changed file with 2 additions and 4 deletions. Show diff stats Hide diff stats

  1. 6  sprintf.c
6  sprintf.c
@@ -628,10 +628,11 @@ rb_str_format(int argc, const VALUE *argv, VALUE fmt)
628 628
 		    if (named_flag) {
629 629
 			rb_raise(rb_eArgError, "named given twice");
630 630
 		    }
631  
-		    if (ref_type != 0) {
  631
+		    if (ref_type == ABS_REF) {
632 632
 			rb_raise(rb_eArgError, "named after numbered");
633 633
 		    }
634 634
 		    named_flag = true;
  635
+		    ref_type = 0;
635 636
 		    SET_REF_TYPE(NAMED_REF);
636 637
 		    arg = get_named_arg(format_str, format_len, &i,
637 638
 			    GETNTHARG(0));
@@ -893,9 +894,6 @@ rb_str_format(int argc, const VALUE *argv, VALUE fmt)
893 894
 		    i - start + 1, arg);
894 895
 	    format_len += num;
895 896
 	    i += num;
896  
-	    if (ref_type == NAMED_REF) {
897  
-		SET_REF_TYPE(0);
898  
-	    }
899 897
 	    break;
900 898
 	}
901 899
     }

0 notes on commit 53b78ca

Please sign in to comment.
Something went wrong with that request. Please try again.