Skip to content
Permalink
Browse files

Merge pull request #639 from kachick/implement-string,hash-try_convert

Implement String.try_convert, Hash.try_convert
  • Loading branch information
alex committed Apr 20, 2013
2 parents 89a38bd + c0f893c commit 3d4430325e557b5bacfa8e2ae9110b57b728decc
@@ -1,8 +1 @@
fails:Hash.try_convert returns the argument if it's a Hash
fails:Hash.try_convert returns the argument if it's a kind of Hash
fails:Hash.try_convert returns nil when the argument does not respond to #to_hash
fails:Hash.try_convert sends #to_hash to the argument and returns the result if it's nil
fails:Hash.try_convert sends #to_hash to the argument and returns the result if it's a Hash
fails:Hash.try_convert sends #to_hash to the argument and returns the result if it's a kind of Hash
fails:Hash.try_convert sends #to_hash to the argument and raises TypeError if it's not a kind of Hash
fails:Hash.try_convert does not rescue exceptions raised by #to_hash
@@ -1,8 +1 @@
fails:String.try_convert returns the argument if it's a String
fails:String.try_convert returns the argument if it's a kind of String
fails:String.try_convert returns nil when the argument does not respond to #to_str
fails:String.try_convert sends #to_str to the argument and returns the result if it's nil
fails:String.try_convert sends #to_str to the argument and returns the result if it's a String
fails:String.try_convert sends #to_str to the argument and returns the result if it's a kind of String
fails:String.try_convert sends #to_str to the argument and raises TypeError if it's not a kind of String
fails:String.try_convert does not rescue exceptions raised by #to_str
@@ -30,6 +30,12 @@ def singleton_method_subscript(self, space, w_obj=None):
result.contents.update(w_res.contents)
return result

@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")
def method_initialize(self, space, w_default=None, block=None):
if w_default is not None:
@@ -401,6 +401,12 @@ def singleton_method_allocate(self, space, w_s=None):
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 3d44303

Please sign in to comment.
You can’t perform that action at this time.