Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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
@Watson1978 Watson1978 authored
Showing with 2 additions and 4 deletions.
  1. +2 −4 sprintf.c
View
6 sprintf.c
@@ -628,10 +628,11 @@ rb_str_format(int argc, const VALUE *argv, VALUE fmt)
if (named_flag) {
rb_raise(rb_eArgError, "named given twice");
}
- if (ref_type != 0) {
+ if (ref_type == ABS_REF) {
rb_raise(rb_eArgError, "named after numbered");
}
named_flag = true;
+ ref_type = 0;
SET_REF_TYPE(NAMED_REF);
arg = get_named_arg(format_str, format_len, &i,
GETNTHARG(0));
@@ -893,9 +894,6 @@ rb_str_format(int argc, const VALUE *argv, VALUE fmt)
i - start + 1, arg);
format_len += num;
i += num;
- if (ref_type == NAMED_REF) {
- SET_REF_TYPE(0);
- }
break;
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.