Impl. Kernel#String, #Hash, #Integer
@@ -40,13 +40,6 @@ module Kernel
e.value
end
- # Placeholder for Array spec to pass
- def Integer(arg, base=0, exception: true)
- return arg.to_int if arg.respond_to?(:toint)
- return arg.to_i if arg.respond_to?(:to_i)
- 0
- end
-
def Array(arg) # rubocop:disable Naming/MethodName
return arg if arg.is_a?(Array)
@@ -60,6 +53,42 @@ module Kernel
raise TypeError
end
+ def Hash(arg) # rubocop:disable Naming/MethodName
+ return arg if arg.is_a?(Hash)
+ return {} if arg.nil? || arg == []
+
+ if arg.respond_to?(:to_hash)
+ ret = arg.to_hash
+ return {} if ret.nil? || ret.empty?
+ return ret if ret.is_a?(Hash)
+ end
+
+ raise TypeError
+ end
+
+ # TODO: Not fully implemented. Just enough for Array spec to pass.
+ def Integer(arg, base = 0, exception: true) # rubocop:disable Naming/MethodName
+ raise ArgumentError, "invalid radix #{base}" unless base.zero? || (2..36).include?(base)
+ raise ArgumentError, 'base specified for non string value' if base.positive? && arg.is_a?(String)
+
+ return arg.to_int if arg.respond_to?(:to_int)
+ return arg.to_i if arg.respond_to?(:to_i)
+
+ 0
+ end
+
+ def String(arg) # rubocop:disable Naming/MethodName
+ return arg if arg.is_a?(String)
+ # TODO: after defined? keyword is implemented
+ # if `to_s` is not defined, raise TypeError even if respond_to?(:to_s) return true
+ if arg.respond_to?(:to_s)
+ ret = arg.to_s
+ return ret if ret.is_a?(String)
+ end
+
+ raise TypeError
+ end
+
# Throws an object, uncaught throws will bubble up through other catch blocks.
#
# @param [Symbol] tag tag being thrown
