Skip to content

Commit

Permalink
added tointeger() YCP builtin
Browse files Browse the repository at this point in the history
  • Loading branch information
lslezak committed Jan 24, 2013
1 parent 0df63f5 commit cddc4e8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/ruby/ycp/builtins.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

###########################################################
Expand Down
6 changes: 6 additions & 0 deletions tests/ruby/builtins_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit cddc4e8

Please sign in to comment.