From c221fbcea9a87e6c5766027553540a5dd21801e9 Mon Sep 17 00:00:00 2001 From: Tim Morgan Date: Tue, 1 Mar 2022 06:53:54 -0600 Subject: [PATCH] Use Object::to_int() helper method for conversion --- src/string_object.cpp | 20 +++----------------- 1 file changed, 3 insertions(+), 17 deletions(-) diff --git a/src/string_object.cpp b/src/string_object.cpp index 3d292ec31..0170a9cce 100644 --- a/src/string_object.cpp +++ b/src/string_object.cpp @@ -101,33 +101,19 @@ String create_padding(String padding, size_t length) { } Value StringObject::center(Env *env, Value length, Value padstr) { - nat_int_t length_i; - auto to_int = "to_int"_s; - - if (length->is_integer()) { - length_i = length->as_integer()->to_nat_int_t(); - } else if (length->respond_to(env, to_int)) { - auto result = length->send(env, to_int); - - if (!result->is_integer()) - env->raise("TypeError", "length can't be converted to an integer"); - - length_i = result->as_integer()->to_nat_int_t(); - } else { - env->raise("TypeError", "length can't be converted to an integer."); - } + nat_int_t length_i = length->to_int(env)->to_nat_int_t(); auto to_str = "to_str"_s; String pad; - if (! padstr) { + if (!padstr) { pad = new String { " " }; } else if (padstr->is_string()) { pad = padstr->as_string()->string(); } else if (padstr->respond_to(env, to_str)) { auto result = padstr->send(env, to_str); - if (! result->is_string()) + if (!result->is_string()) env->raise("TypeError", "padstr can't be converted to a string"); pad = result->as_string()->string();