Skip to content

Commit

Permalink
* object.c (rb_Hash): trivial optimization.
Browse files Browse the repository at this point in the history
* test/ruby/test_object.rb (TestObject#test_convert_hash): fix
  arguments order.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@34368 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
  • Loading branch information
nobu committed Jan 24, 2012
1 parent 498838c commit d03199b
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
2 changes: 1 addition & 1 deletion object.c
Original file line number Diff line number Diff line change
Expand Up @@ -2600,7 +2600,7 @@ rb_Hash(VALUE val)
if (NIL_P(val)) return rb_hash_new();
VALUE tmp = rb_check_hash_type(val);
if (NIL_P(tmp)) {
if (TYPE(val) == T_ARRAY && RARRAY_LEN(val) == 0)
if (RB_TYPE_P(val, T_ARRAY) && RARRAY_LEN(val) == 0)
return rb_hash_new();
rb_raise(rb_eTypeError, "can't convert %s into Hash", rb_obj_classname(val));
}
Expand Down
8 changes: 4 additions & 4 deletions test/ruby/test_object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -198,14 +198,14 @@ def o.respond_to?(*) false; end
end

def test_convert_hash
assert_equal(Hash(nil), {})
assert_equal(Hash([]), {})
assert_equal(Hash(key: :value), {key: :value})
assert_equal({}, Hash(nil))
assert_equal({}, Hash([]))
assert_equal({key: :value}, Hash(key: :value))
assert_raise(TypeError) { Hash([1,2]) }
assert_raise(TypeError) { Hash(Object.new) }
o = Object.new
def o.to_hash; {a: 1, b: 2}; end
assert_equal(Hash(o), {a: 1, b: 2})
assert_equal({a: 1, b: 2}, Hash(o))
def o.to_hash; 9; end
assert_raise(TypeError) { Hash(o) }
end
Expand Down

0 comments on commit d03199b

Please sign in to comment.