~ubuntu-branches/ubuntu/trusty/ruby1.9/trusty

« back to all changes in this revision

Viewing changes to object.c

  • Committer: Bazaar Package Importer
  • Author(s): Lucas Nussbaum
  • Date: 2006-05-08 22:23:12 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20060508222312-w2wqeaz030ifi59j
Tags: 1.9.0+20060423-3ubuntu1
* Resynchronized with Debian.
* Only change from Debian is the addition of
  debian/patches/903_sparc_fix_define.patch to fix illegal instructions
  at runtime on sparc. (change from 1.9.0+20050921-1ubuntu1)

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
 
3
3
  object.c -
4
4
 
5
 
  $Author: ocean $
6
 
  $Date: 2005/09/12 10:44:20 $
 
5
  $Author: akr $
 
6
  $Date: 2006/03/02 05:22:30 $
7
7
  created at: Thu Jul 15 12:01:24 JST 1993
8
8
 
9
9
  Copyright (C) 1993-2003 Yukihiro Matsumoto
95
95
    return Qfalse;
96
96
}
97
97
 
98
 
 
99
 
/*
100
 
 *  Document-method: __id__
101
 
 *  Document-method: object_id
102
 
 *
103
 
 *  call-seq:
104
 
 *     obj.__id__       => fixnum
105
 
 *     obj.object_id    => fixnum
106
 
 *  
107
 
 *  Returns an integer identifier for <i>obj</i>. The same number will
108
 
 *  be returned on all calls to <code>id</code> for a given object, and
109
 
 *  no two active objects will share an id.
110
 
 *  <code>Object#object_id</code> is a different concept from the
111
 
 *  <code>:name</code> notation, which returns the symbol id of
112
 
 *  <code>name</code>. Replaces the deprecated <code>Object#id</code>.
113
 
 */
114
 
 
115
 
 
116
 
 
117
 
/*
118
 
 *  call-seq:
119
 
 *     obj.hash    => fixnum
120
 
 *  
121
 
 *  Generates a <code>Fixnum</code> hash value for this object. This
122
 
 *  function must have the property that <code>a.eql?(b)</code> implies
123
 
 *  <code>a.hash == b.hash</code>. The hash value is used by class
124
 
 *  <code>Hash</code>. Any hash value that exceeds the capacity of a
125
 
 *  <code>Fixnum</code> will be truncated before being used.
126
 
 */
127
 
 
128
 
VALUE
129
 
rb_obj_id(VALUE obj)
130
 
{
131
 
    if (SPECIAL_CONST_P(obj)) {
132
 
        return LONG2NUM((long)obj);
133
 
    }
134
 
    return (VALUE)((long)obj|FIXNUM_FLAG);
135
 
}
136
 
 
137
98
VALUE
138
99
rb_class_real(VALUE cl)
139
100
{
737
698
 */
738
699
 
739
700
static VALUE
740
 
nil_to_a(VALUE obj)
 
701
nil_to_a(obj)
 
702
    VALUE obj;
741
703
{
742
704
    return rb_ary_new2(0);
743
705
}
1052
1014
    str = rb_str_new(0, strlen(name)+1);
1053
1015
    RSTRING(str)->ptr[0] = ':';
1054
1016
    strcpy(RSTRING(str)->ptr+1, name);
1055
 
    if (rb_is_junk_id(id)) {
 
1017
    if (!rb_symname_p(name)) {
1056
1018
        str = rb_str_dump(str);
1057
1019
        strncpy(RSTRING(str)->ptr, ":\"", 2);
1058
1020
    }
1627
1589
 
1628
1590
/*
1629
1591
 *  call-seq:
1630
 
 *     mod.const_get(sym)    => obj
 
1592
 *     mod.const_get(sym, inherit=true)    => obj
1631
1593
 *  
1632
1594
 *  Returns the value of the named constant in <i>mod</i>.
1633
1595
 *     
1634
1596
 *     Math.const_get(:PI)   #=> 3.14159265358979
 
1597
 *
 
1598
 *  If the constant is not defined or is defined by the ancestors and
 
1599
 *  +inherit+ is false, +NameError+ will be raised.
1635
1600
 */
1636
1601
 
1637
1602
static VALUE
1638
 
rb_mod_const_get(VALUE mod, VALUE name)
 
1603
rb_mod_const_get(int argc, VALUE *argv, VALUE mod)
1639
1604
{
1640
 
    ID id = rb_to_id(name);
 
1605
    VALUE name, recur;
 
1606
    ID id;
1641
1607
 
 
1608
    if (argc == 1) {
 
1609
        name = argv[0];
 
1610
        recur = Qtrue;
 
1611
    }
 
1612
    else {
 
1613
        rb_scan_args(argc, argv, "11", &name, &recur);
 
1614
    }
 
1615
    id = rb_to_id(name);
1642
1616
    if (!rb_is_const_id(id)) {
1643
1617
        rb_name_error(id, "wrong constant name %s", rb_id2name(id));
1644
1618
    }
1645
 
    return rb_const_get(mod, id);
 
1619
    return RTEST(recur) ? rb_const_get(mod, id) : rb_const_get_at(mod, id);
1646
1620
}
1647
1621
 
1648
1622
/*
1671
1645
 
1672
1646
/*
1673
1647
 *  call-seq:
1674
 
 *     mod.const_defined?(sym)   => true or false
 
1648
 *     mod.const_defined?(sym, inherit=true)   => true or false
1675
1649
 *  
1676
1650
 *  Returns <code>true</code> if a constant with the given name is
1677
 
 *  defined by <i>mod</i>.
 
1651
 *  defined by <i>mod</i>, or its ancestors if +inherit+ is not false.
1678
1652
 *     
1679
1653
 *     Math.const_defined? "PI"   #=> true
 
1654
 *     IO.const_defined? "SYNC"   #=> true
 
1655
 *     IO.const_defined? "SYNC", false   #=> false
1680
1656
 */
1681
1657
 
1682
1658
static VALUE
1683
 
rb_mod_const_defined(VALUE mod, VALUE name)
 
1659
rb_mod_const_defined(int argc, VALUE *argv, VALUE mod)
1684
1660
{
1685
 
    ID id = rb_to_id(name);
 
1661
    VALUE name, recur;
 
1662
    ID id;
1686
1663
 
 
1664
    if (argc == 1) {
 
1665
        name = argv[0];
 
1666
        recur = Qtrue;
 
1667
    }
 
1668
    else {
 
1669
        rb_scan_args(argc, argv, "11", &name, &recur);
 
1670
    }
 
1671
    id = rb_to_id(name);
1687
1672
    if (!rb_is_const_id(id)) {
1688
1673
        rb_name_error(id, "wrong constant name %s", rb_id2name(id));
1689
1674
    }
1690
 
    return rb_const_defined_at(mod, id);
 
1675
    return RTEST(recur) ? rb_const_defined(mod, id) : rb_const_defined_at(mod, id);
1691
1676
}
1692
1677
 
1693
1678
/*
2251
2236
    VALUE tmp = rb_check_array_type(val);
2252
2237
 
2253
2238
    if (NIL_P(tmp)) {
2254
 
        tmp = rb_check_convert_type(val, T_ARRAY, "Array", "to_a");
2255
 
        if (NIL_P(tmp)) {
2256
 
            return rb_ary_new3(1, val);
2257
 
        }
 
2239
        return rb_convert_type(val, T_ARRAY, "Array", "to_a");
2258
2240
    }
2259
2241
    return tmp;
2260
2242
}
2265
2247
 *  
2266
2248
 *  Returns <i>arg</i> as an <code>Array</code>. First tries to call
2267
2249
 *  <i>arg</i><code>.to_ary</code>, then <i>arg</i><code>.to_a</code>.
2268
 
 *  If both fail, creates a single element array containing <i>arg</i>
2269
 
 *  (unless <i>arg</i> is <code>nil</code>).
2270
2250
 *     
2271
2251
 *     Array(1..5)   #=> [1, 2, 3, 4, 5]
2272
2252
 */
2399
2379
 
2400
2380
    rb_define_method(rb_mKernel, "eql?", rb_obj_equal, 1);
2401
2381
 
2402
 
    rb_define_method(rb_mKernel, "hash", rb_obj_id, 0);
2403
 
    rb_define_method(rb_mKernel, "__id__", rb_obj_id, 0);
2404
 
    rb_define_method(rb_mKernel, "object_id", rb_obj_id, 0);
2405
2382
    rb_define_method(rb_mKernel, "class", rb_obj_class, 0);
2406
2383
 
2407
2384
    rb_define_method(rb_mKernel, "clone", rb_obj_clone, 0);
2502
2479
                     rb_class_private_instance_methods, -1);   /* in class.c */
2503
2480
 
2504
2481
    rb_define_method(rb_cModule, "constants", rb_mod_constants, 0); /* in variable.c */
2505
 
    rb_define_method(rb_cModule, "const_get", rb_mod_const_get, 1);
 
2482
    rb_define_method(rb_cModule, "const_get", rb_mod_const_get, -1);
2506
2483
    rb_define_method(rb_cModule, "const_set", rb_mod_const_set, 2);
2507
 
    rb_define_method(rb_cModule, "const_defined?", rb_mod_const_defined, 1);
 
2484
    rb_define_method(rb_cModule, "const_defined?", rb_mod_const_defined, -1);
2508
2485
    rb_define_private_method(rb_cModule, "remove_const", 
2509
2486
                             rb_mod_remove_const, 1); /* in variable.c */
2510
2487
    rb_define_method(rb_cModule, "const_missing", 
2528
2505
    rb_cData = rb_define_class("Data", rb_cObject);
2529
2506
    rb_undef_alloc_func(rb_cData);
2530
2507
 
 
2508
    rb_global_variable(&ruby_top_self);
2531
2509
    ruby_top_self = rb_obj_alloc(rb_cObject);
2532
 
    rb_global_variable(&ruby_top_self);
2533
2510
    rb_define_singleton_method(ruby_top_self, "to_s", main_to_s, 0);
2534
2511
 
2535
2512
    rb_cTrueClass = rb_define_class("TrueClass", rb_cObject);