From cddc4e86774bc153a563d924bd15b547b7bf9d27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ladislav=20Slez=C3=A1k?= Date: Thu, 24 Jan 2013 19:00:37 +0100 Subject: [PATCH] added tointeger() YCP builtin --- src/ruby/ycp/builtins.rb | 12 ++++++++++-- tests/ruby/builtins_test.rb | 6 ++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/ruby/ycp/builtins.rb b/src/ruby/ycp/builtins.rb index e06f6870..ba90e772 100644 --- a/src/ruby/ycp/builtins.rb +++ b/src/ruby/ycp/builtins.rb @@ -183,8 +183,16 @@ def self.tofloat ########################################################### # Converts a value to an integer. - def self.tointeger - raise "Builtin tointeger() is not implemented yet" + def self.tointeger object + return nil if object.nil? + + case object + # use full qualified ::Float to avoid clash with YCP::Builtins::Float + when String, ::Float, Fixnum, Bignum + object.to_i + else + nil + end end ########################################################### diff --git a/tests/ruby/builtins_test.rb b/tests/ruby/builtins_test.rb index 1e9d2899..f7989782 100644 --- a/tests/ruby/builtins_test.rb +++ b/tests/ruby/builtins_test.rb @@ -259,4 +259,10 @@ def test_srandom assert_equal nil, YCP::Builtins.srandom(10) end + def test_tointeger() + assert_equal nil, YCP::Builtins.tointeger(nil) + assert_equal 120, YCP::Builtins.tointeger(120) + assert_equal 120, YCP::Builtins.tointeger("120") + assert_equal 120, YCP::Builtins.tointeger(120.0) + end end