The problem is that gsub returns 2 values, the second value being the number of matches. So you are calling to_number("12345",1) . The second arg to to_number() is the base, and since this isn't valid, it returns nil.
I followed Lua in having to_number() return a nil when there is an error, but maybe I should throw an error. The error in this case would be "radix 1 less than Character.MIN_RADIX" (from java). What do you think?