Ruby: Add method KernAux.sprintf

This commit is contained in:
Alex Kotov 2022-01-21 19:52:46 +05:00
parent b30a98d9b3
commit 9193b9e9ea
Signed by: kotovalexarian
GPG Key ID: 553C0EBBEB5D5F08
2 changed files with 21 additions and 0 deletions

View File

@ -20,6 +20,16 @@ module KernAux
assert_do file, Integer(line), msg
end
def self.sprintf(*args)
args.map do |arg|
if arg.is_a? Array
sprintf1(*arg)
else
arg
end
end.join.freeze
end
def self.sprintf1(format, arg = nil)
if arg.nil?
snprintf1(SPRINTF1_BUFFER_SIZE, format).first

View File

@ -0,0 +1,11 @@
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe KernAux, '.sprintf' do
subject(:sprintf) { described_class.sprintf 'Hello, ', ['%s', 'World'], '!' }
it { is_expected.to be_instance_of String }
it { is_expected.to be_frozen }
it { is_expected.to eq 'Hello, World!' }
end