Project

General

Profile

Feature #3131 ยป kernel-hash.patch

runpaint (Run Paint Run Run), 09/02/2010 11:03 PM

View differences:

object.c
return rb_Array(arg);
}
VALUE
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)
return rb_hash_new();
rb_raise(rb_eTypeError, "can't convert %s into Hash", rb_obj_classname(val));
}
return tmp;
}
/*
* call-seq:
* Hash(arg) -> hash
*
* Converts <i>arg</i> to a <code>Hash</code> by calling
* <i>arg</i><code>.to_hash</code>. Returns an empty <code>Hash</code> when
* <i>arg</i> is <tt>nil</tt> or <tt>[]</tt>.
*
* Hash([]) #=> {}
* Hash(nil) #=> nil
* Hash(key: :value) #=> {:key => :value}
* Hash([1, 2, 3]) #=> TypeError
*/
static VALUE
rb_f_hash(VALUE obj, VALUE arg)
{
return rb_Hash(arg);
}
/*
* Document-class: Class
*
......
rb_define_global_function("String", rb_f_string, 1);
rb_define_global_function("Array", rb_f_array, 1);
rb_define_global_function("Hash", rb_f_hash, 1);
rb_cNilClass = rb_define_class("NilClass", rb_cObject);
rb_define_method(rb_cNilClass, "to_i", nil_to_i, 0);
test/ruby/test_object.rb
assert_raise(TypeError) { Array(o) }
end
def test_convert_hash
assert_equal(Hash(nil), {})
assert_equal(Hash([]), {})
assert_equal(Hash(key: :value), {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})
def o.to_hash; 9; end
assert_raise(TypeError) { Hash(o) }
end
def test_to_integer
o = Object.new
def o.to_i; nil; end
    (1-1/1)