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
557 557
VALUE rb_String(VALUE);
558 558
VALUE rb_Array(VALUE);
559 559
VALUE rb_Hash(VALUE);
560
VALUE rb_Symbol(VALUE);
560 561
double rb_cstr_to_dbl(const char*, int);
561 562
double rb_str_to_dbl(VALUE, int);
562 563
/* parse.y */
object.c
2662 2662
}
2663 2663

  
2664 2664
/*
2665
 *  call-seq:
2666
 *     Symbol(arg)   -> Symbol
2667
 *
2668
 *  Converts <i>arg</i> to a <code>Symbol</code> by calling its
2669
 *  <code>to_sym</code> method.
2670
 *
2671
 *     Symbol("string")    #=> :string
2672
 *     Symbol(123456)      #=> :"123456"
2673
 */
2674

  
2675
static VALUE
2676
rb_f_symbol(VALUE obj, VALUE arg)
2677
{
2678
    return rb_Symbol(arg);
2679
}
2680

  
2681
VALUE
2682
rb_Symbol(VALUE val)
2683
{
2684
    return rb_convert_type(val, T_SYMBOL, "Symbol", "to_sym");
2685
}
2686

  
2687
/*
2665 2688
 *  Document-class: Class
2666 2689
 *
2667 2690
 *  Classes in Ruby are first-class objects---each is an instance of
......
2920 2943
    rb_define_global_function("String", rb_f_string, 1);
2921 2944
    rb_define_global_function("Array", rb_f_array, 1);
2922 2945
    rb_define_global_function("Hash", rb_f_hash, 1);
2946
    rb_define_global_function("Symbol", rb_f_symbol, 1);
2923 2947

  
2924 2948
    rb_cNilClass = rb_define_class("NilClass", rb_cObject);
2925 2949
    rb_define_method(rb_cNilClass, "to_i", nil_to_i, 0);
test/ruby/test_object.rb
210 210
    assert_raise(TypeError) { Hash(o) }
211 211
  end
212 212

  
213
  def test_convert_symbol
214
    o = Object.new
215
    def o.to_sym; "foo"; end
216
    assert_raise(TypeError) { Symbol(o) }
217
    def o.to_sym; :foo; end
218
    assert_equal(:foo, Symbol(o))
219
    def o.respond_to?(*) false; end
220
    assert_raise(TypeError) { Symbol(o) }
221
  end
222

  
213 223
  def test_to_integer
214 224
    o = Object.new
215 225
    def o.to_i; nil; end
216
-