Module: Altprintf

Defined in:
lib/altprintf/version.rb,
ext/altprintf/ext.c

Constant Summary collapse

LIB_VERSION =

The version of libaltprintf that this extension was compiled against.

ver

Class Method Summary collapse

Class Method Details

.fmt(*argv, self) ⇒ Object

fmt(format_string [, arguments…]) -> String

Formats a ‘format_string` with `arguments`. `format_string` may be any vaild altprintf format string . Additionally, a hash may be specified as the last argument, or as keyword arguments and the values of the hash may be directly accessed in the format string using < and >.

For example: fmt(“hello %<name>”, name: “John”) #=> “hello John”

Note that the keys within <> are always assumed to be symbols, so the following would not work fmt(“hello %<name>”, { “name” => “John” }) #=> “hello John”



232
233
234
235
# File 'ext/altprintf/ext.c', line 232

VALUE rb_altprintf_single_pass(size_t argc, VALUE *argv, VALUE self)
{
	return rb_altprintf(1, argc, argv, self);
}

.fmtm(passes, format_string[, arguments...]) ⇒ String

Same as #fmt, but takes an additional argument, passes, which specifies the number of passes to go over the format string.

For example: fmtm(0, “%%%%%%%%”) #=> “%%%%%%%%” fmtm(1, “%%%%%%%%”) #=> “%%%%” fmtm(2, “%%%%%%%%”) #=> “%%” fmtm(3, “%%%%%%%%”) #=> “%” fmtm(4, “%%%%%%%%”) #=> “”

Returns:

  • (String)


253
254
255
256
257
258
259
260
261
262
263
264
# File 'ext/altprintf/ext.c', line 253

VALUE rb_altprintf_multi_pass(size_t argc, VALUE *argv, VALUE self)
{
	long passes;

	Check_Type(argv[0], T_FIXNUM);
	passes = FIX2LONG(argv[0]);
	if (passes < 0)
		rb_raise(rb_eArgError, "expected positive number of passes");

	LOG("passes: %ld\n", passes);
	return rb_altprintf(passes, argc - 1, &argv[1], self);
}