Project

General

Profile

Feature #6697 ยป 0001-Add-Kernel-Symbol-conversion-method-like-String-Arra.patch

madeofcode (Mark Dodwell), 07/04/2012 06:25 PM

View differences:

include/ruby/intern.h
VALUE rb_String(VALUE);
VALUE rb_Array(VALUE);
VALUE rb_Hash(VALUE);
VALUE rb_Symbol(VALUE);
double rb_cstr_to_dbl(const char*, int);
double rb_str_to_dbl(VALUE, int);
/* parse.y */
object.c
}
/*
* call-seq:
* Symbol(arg) -> Symbol
*
* Converts <i>arg</i> to a <code>Symbol</code> by calling its
* <code>to_sym</code> method.
*
* Symbol("string") #=> :string
* Symbol(123456) #=> :"123456"
*/
static VALUE
rb_f_symbol(VALUE obj, VALUE arg)
{
return rb_Symbol(arg);
}
VALUE
rb_Symbol(VALUE val)
{
return rb_convert_type(val, T_SYMBOL, "Symbol", "to_sym");
}
/*
* Document-class: Class
*
* Classes in Ruby are first-class objects---each is an instance of
......
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_define_global_function("Symbol", rb_f_symbol, 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) { Hash(o) }
end
def test_convert_symbol
o = Object.new
def o.to_sym; "foo"; end
assert_raise(TypeError) { Symbol(o) }
def o.to_sym; :foo; end
assert_equal(:foo, Symbol(o))
def o.respond_to?(*) false; end
assert_raise(TypeError) { Symbol(o) }
end
def test_to_integer
o = Object.new
def o.to_i; nil; end
    (1-1/1)