Skip to content

Commit

Permalink
Implement Hash.try_convert
Browse files Browse the repository at this point in the history
  • Loading branch information
kachick committed Apr 20, 2013
1 parent 1f1082e commit c0f893c
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 7 deletions.
7 changes: 0 additions & 7 deletions spec/tags/core/hash/try_convert_tags.txt
@@ -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
6 changes: 6 additions & 0 deletions topaz/objects/hashobject.py
Expand Up @@ -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:
Expand Down

0 comments on commit c0f893c

Please sign in to comment.