Project

General

Profile

Actions

Feature #4043

closed

グローバル関数current_classの提案

Added by metanest (Makoto Kishimoto) over 13 years ago. Updated over 11 years ago.

Status:
Rejected
Target version:
[ruby-dev:42564]

Description

=begin
カレントクラス(MRIの内部でいうところのcref)をRubyから調べられるメソッドcurrent_classを提案します。
グローバル関数(Kernelのプライベートメソッド)にする理由は、self(ないしなんらかのオブジェクト)とは関係ない値を返すメソッドであるため、です。
ユースケースですが、例えば以下のようにして、モジュールのinstance_evalとmodule_evalの違いをRubyプログラマが確かめることができ、Rubyの理解に役立ちます。

$ irb19
irb(main):001:0> module M ; end
=> nil
irb(main):002:0> M.instance_eval { current_class }
=> #Class:M
irb(main):003:0> M.module_eval { current_class }
=> M

差分を付けますが、クイックハックですので修正が必要かと思います。

diff --git a/object.c b/object.c
index 75192c1..82ad85a 100644
--- a/object.c
+++ b/object.c
@@ -2408,6 +2408,13 @@ rb_f_array(VALUE obj, VALUE arg)
return rb_Array(arg);
}

+VALUE rb_vm_cref_cls(void);
+static VALUE
+rb_f_current_class(VALUE obj)
+{

  • return rb_vm_cref_cls();
    +}

/*

  • Document-class: Class

@@ -2588,6 +2595,8 @@ Init_Object(void)
rb_define_global_function("String", rb_f_string, 1);
rb_define_global_function("Array", rb_f_array, 1);

  • rb_define_global_function("current_class", rb_f_current_class, 0);
  • rb_cNilClass = rb_define_class("NilClass", rb_cObject);
    rb_define_method(rb_cNilClass, "to_i", nil_to_i, 0);
    rb_define_method(rb_cNilClass, "to_f", nil_to_f, 0);
    diff --git a/vm.c b/vm.c
    index 980e7ea..dcc43df 100644
    --- a/vm.c
    +++ b/vm.c
    @@ -819,6 +819,12 @@ rb_vm_cref(void)
    return vm_get_cref(cfp->iseq, cfp->lfp, cfp->dfp);
    }

+VALUE
+rb_vm_cref_cls(void)
+{

  • return rb_vm_cref()->nd_clss;
    +}

#if 0
void
debug_cref(NODE *cref)
=end


Files

patch-cref (1020 Bytes) patch-cref metanest (Makoto Kishimoto), 11/11/2010 12:50 PM
patch-current-class (2.17 KB) patch-current-class metanest (Makoto Kishimoto), 02/11/2011 05:53 PM
No4043.pdf (14.3 KB) No4043.pdf [ruby-dev:45708] コンペ向けの資料 metanest (Makoto Kishimoto), 06/24/2012 05:10 PM
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0