Permalink
Browse files

Merge pull request #753 from kostya/convert_type

refactor convert_types
  • Loading branch information...
alex committed Jun 17, 2013
2 parents ed9d32c + ac3f2f9 commit 0007fa4f780bebefc8d2f6550fa0298e1f8ef3ad
@@ -1,14 +1,10 @@
class Array
def initialize(size_or_arr = nil, obj = nil, &block)
self.clear
if size_or_arr.nil?
return self
end
return self if size_or_arr.nil?
if obj.nil?
if size_or_arr.kind_of?(Array)
return self.replace(size_or_arr)
elsif size_or_arr.respond_to?(:to_ary)
return self.replace(size_or_arr.to_ary)
if ary = Topaz.try_convert_type(size_or_arr, Array, :to_ary)
return self.replace(ary)
end
end
length = Topaz.convert_type(size_or_arr, Fixnum, :to_int)
@@ -512,4 +508,8 @@ def transpose
end
out
end

def self.try_convert(arg)
Topaz.try_convert_type(arg, Array, :to_ary)
end
end
@@ -362,7 +362,7 @@ def collect_concat(&block)
out = []
self.each do |e|
v = yield(e)
if v.respond_to?(:to_ary) && ary = Array.try_convert(v)
if ary = Array.try_convert(v)
out.concat(ary)
else
out << v
@@ -85,7 +85,7 @@ def eql?(other)

def merge!(other, &block)
raise RuntimeError.new("can't modify frozen #{self.class}") if frozen?
other = other.to_hash unless other.kind_of? Hash
other = Topaz.convert_type(other, Hash, :to_hash)
if block
other.each do |key, val|
if has_key? key
@@ -224,4 +224,8 @@ def flatten(level = 1)
Topaz::Array.flatten(self, out, level)
out
end

def self.try_convert(arg)
Topaz.try_convert_type(arg, Hash, :to_hash)
end
end
@@ -148,4 +148,8 @@ def self.popen(cmd, mode = 'r', opts = {}, &block)
def pid
@pid
end

def self.try_convert(arg)
Topaz.try_convert_type(arg, IO, :to_io)
end
end
@@ -30,7 +30,7 @@ def Array(arg)
end

def String(arg)
arg.to_s
Topaz.convert_type(arg, String, :to_s)
end
module_function :String

@@ -52,8 +52,7 @@ def loop(&block)
end

def `(cmd)
cmd = cmd.to_str if cmd.respond_to?(:to_str)
raise TypeError.new("can't convert #{cmd.class} into String") unless cmd.is_a?(String)
cmd = Topaz.convert_type(cmd, String, :to_str)
res = ''
IO.popen(cmd) do |r|
res << r.read
@@ -32,11 +32,7 @@ def capitalize
end

def casecmp(other)
unless other.respond_to?(:to_str)
raise TypeError.new("can't convert #{other.class} into String")
end

other = other.to_str
other = Topaz.convert_type(other, String, :to_str)
diff = self.length - other.length
short = diff < 0
long = diff > 0
@@ -59,8 +55,8 @@ def empty?

def start_with?(*prefixes)
prefixes.any? do |prefix|
next false unless prefix.respond_to?(:to_str)
prefix = prefix.to_str
prefix = Topaz.try_convert_type(prefix, String, :to_str)
next false unless prefix
prelen = prefix.length
next false if prelen > self.length
0.upto(prelen - 1).all? { |index| self[index] == prefix[index] }
@@ -69,8 +65,8 @@ def start_with?(*prefixes)

def end_with?(*suffixes)
suffixes.any? do |suffix|
next false unless suffix.respond_to?(:to_str)
suffix = suffix.to_str
suffix = Topaz.try_convert_type(suffix, String, :to_str)
next false unless suffix
suflen = suffix.length
next false if suflen > self.length
(-suflen).upto(-1).all? { |index| self[index] == suffix[index] }
@@ -170,4 +166,8 @@ def replace(other)
clear
insert(0, other)
end

def self.try_convert(arg)
Topaz.try_convert_type(arg, String, :to_str)
end
end
@@ -5,7 +5,7 @@ def self.flatten(array, out, level)
array.each do |e|
if level == 0
out << e
elsif e.respond_to?(:to_ary) && ary = ::Array.try_convert(e)
elsif ary = ::Array.try_convert(e)
modified = true
flatten(ary, out, level - 1)
else
@@ -1,6 +1 @@
fails:IO.try_convert returns the passed IO object
fails:IO.try_convert does not call #to_io on an IO instance
fails:IO.try_convert calls #to_io to coerce an object
fails:IO.try_convert returns nil when the passed object does not respond to #to_io
fails:IO.try_convert raises a TypeError if the object does not return an IO from #to_io
fails:IO.try_convert propagates an exception raised by #to_io
@@ -1,13 +1,3 @@
fails:Kernel.String raises a TypeError if #to_s does not exist
fails:Kernel.String raises a TypeError if respond_to? returns false for #to_s
fails:Kernel.String raises a TypeError if #to_s is not defined, even though #respond_to?(:to_s) returns true
fails:Kernel.String raises a TypeError if #to_s does not return a String
fails:Kernel.String returns the same object if it is already a String
fails:Kernel.String returns the same object if it is an instance of a String subclass
fails:Kernel#String raises a TypeError if #to_s does not exist
fails:Kernel#String raises a TypeError if respond_to? returns false for #to_s
fails:Kernel#String raises a TypeError if #to_s is not defined, even though #respond_to?(:to_s) returns true
fails:Kernel#String raises a TypeError if #to_s does not return a String
fails:Kernel#String returns the same object if it is already a String
fails:Kernel#String returns the same object if it is an instance of a String subclass
fails:Kernel#String is a private method
@@ -229,12 +229,6 @@ def method_join(self, space, w_sep=None):
for w_o in self.items_w
]))

@classdef.singleton_method("try_convert")
def method_try_convert(self, space, w_obj):
if not space.is_kind_of(w_obj, space.w_array):
w_obj = space.convert_type(w_obj, space.w_array, "to_ary", raise_error=False)
return w_obj

@classdef.method("pop")
@check_frozen()
def method_pop(self, space, w_num=None):
@@ -19,12 +19,6 @@ def __init__(self, space, klass=None):
def method_allocate(self, space):
return W_HashObject(space, self)

@classdef.singleton_method("try_convert")
def method_try_convert(self, space, w_obj):
if not space.is_kind_of(w_obj, space.w_hash):
w_obj = space.convert_type(w_obj, space.w_hash, "to_hash", raise_error=False)
return w_obj

@classdef.method("initialize")
@check_frozen()
def method_initialize(self, space, w_default=None, block=None):
@@ -428,12 +428,6 @@ def singleton_method_allocate(self, space):
storage = strategy.erase("")
return W_StringObject(space, storage, strategy, self)

@classdef.singleton_method("try_convert")
def method_try_convert(self, space, w_obj):
if not space.is_kind_of(w_obj, space.w_string):
w_obj = space.convert_type(w_obj, space.w_string, "to_str", raise_error=False)
return w_obj

@classdef.method("initialize")
def method_initialize(self, space, w_s=None):
if w_s is not None:

0 comments on commit 0007fa4

Please sign in to comment.