Permalink
Browse files

allow multiple named argument in String#%. fix #1342

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 } }
  • Loading branch information...
Watson1978 committed Jan 29, 2012
1 parent 31cc69c commit b78d2b6130ff7d2460572025b68434c0fbbf93c3
Showing with 3 additions and 0 deletions.
  1. +3 −0 sprintf.c
View
@@ -878,6 +878,9 @@ 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;
}
}

2 comments on commit b78d2b6

@seanlilmateus

This comment has been minimized.

Show comment Hide comment
@seanlilmateus

seanlilmateus Jan 31, 2012

hi @Watson1978, I just found a small bug in sprintf:
this line don’t work on Macruby, unfortunately I can’t solve it by myself! I hope to be able soon!

   puts "%<foo>02d : %<bar>02d" % { :foo => 1, :bar => 2 } # => 01 : 02

thanks, Mateus

hi @Watson1978, I just found a small bug in sprintf:
this line don’t work on Macruby, unfortunately I can’t solve it by myself! I hope to be able soon!

   puts "%<foo>02d : %<bar>02d" % { :foo => 1, :bar => 2 } # => 01 : 02

thanks, Mateus

@Watson1978

This comment has been minimized.

Show comment Hide comment
@Watson1978

Watson1978 Feb 1, 2012

Member

Thank you, @seanlilmateus. I will try to fix it later

Member

Watson1978 replied Feb 1, 2012

Thank you, @seanlilmateus. I will try to fix it later

Please sign in to comment.