[前][次][番号順一覧][スレッド一覧]

ruby-changes:33096

From: naruse <ko1@a...>
Date: Wed, 26 Feb 2014 01:34:06 +0900 (JST)
Subject: [ruby-changes:33096] naruse:r45175 (trunk): * string.c (sym_find): Add Symbol.find(str), which returns whether given

naruse	2014-02-26 01:34:00 +0900 (Wed, 26 Feb 2014)

  New Revision: 45175

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=45175

  Log:
    * string.c (sym_find): Add Symbol.find(str), which returns whether given
      string is defined as symbol or not. [Feature #7854]

  Modified files:
    trunk/ChangeLog
    trunk/NEWS
    trunk/string.c
    trunk/test/ruby/test_symbol.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 45174)
+++ ChangeLog	(revision 45175)
@@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Wed Feb 26 01:29:27 2014  NARUSE, Yui  <naruse@r...>
+
+	* string.c (sym_find): Add Symbol.find(str), which returns whether given
+	  string is defined as symbol or not. [Feature #7854]
+
 Tue Feb 25 22:52:02 2014  Kazuhiro NISHIYAMA  <zn@m...>
 
 	* ext/dl/dl.c (rb_dl_realloc): use NUM2SIZET instead of NUM2INT.
Index: string.c
===================================================================
--- string.c	(revision 45174)
+++ string.c	(revision 45175)
@@ -8231,6 +8231,27 @@ str_scrub_bang(int argc, VALUE *argv, VA https://github.com/ruby/ruby/blob/trunk/string.c#L8231
 
 /*
  *  call-seq:
+ *     Symbol.find(str)   -> symbol or nil
+ *
+ *  Return the related symbol if the symbol already exists.
+ *  Return nil if not.
+ */
+
+static VALUE
+sym_find(VALUE dummy, VALUE sym)
+{
+    ID id = rb_check_id(&sym);
+
+    if (id) {
+	return ID2SYM(id);
+    }
+    else {
+	return Qnil;
+    }
+}
+
+/*
+ *  call-seq:
  *     sym == obj   -> true or false
  *
  *  Equality---If <i>sym</i> and <i>obj</i> are exactly the same
@@ -8787,6 +8808,7 @@ Init_String(void) https://github.com/ruby/ruby/blob/trunk/string.c#L8808
     rb_undef_alloc_func(rb_cSymbol);
     rb_undef_method(CLASS_OF(rb_cSymbol), "new");
     rb_define_singleton_method(rb_cSymbol, "all_symbols", rb_sym_all_symbols, 0); /* in parse.y */
+    rb_define_singleton_method(rb_cSymbol, "find", sym_find, 1);
 
     rb_define_method(rb_cSymbol, "==", sym_equal, 1);
     rb_define_method(rb_cSymbol, "===", sym_equal, 1);
Index: NEWS
===================================================================
--- NEWS	(revision 45174)
+++ NEWS	(revision 45175)
@@ -21,6 +21,9 @@ with all sufficient information, see the https://github.com/ruby/ruby/blob/trunk/NEWS#L21
     * min_by
     * max
     * max_by
+* Symbol
+  * New methods
+    * Symbol.find(str) returns whether given string is defined as symbol or not.
 
 === Core classes compatibility issues (excluding feature bug fixes)
 
Index: test/ruby/test_symbol.rb
===================================================================
--- test/ruby/test_symbol.rb	(revision 45174)
+++ test/ruby/test_symbol.rb	(revision 45175)
@@ -1,4 +1,5 @@ https://github.com/ruby/ruby/blob/trunk/test/ruby/test_symbol.rb#L1
 require 'test/unit'
+require_relative 'envutil'
 
 class TestSymbol < Test::Unit::TestCase
   # [ruby-core:3573]
@@ -206,4 +207,18 @@ class TestSymbol < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_symbol.rb#L207
     assert_equal(true, "foo#{Time.now.to_i}".to_sym.frozen?)
     assert_equal(true, :foo.to_sym.frozen?)
   end
+
+  def test_sym_find
+    assert_separately(%w[--disable=gems], <<-"end;")
+      assert_equal :intern, Symbol.find("intern")
+      assert_raise(TypeError){ Symbol.find(true) }
+
+      str = "__noexistent__"
+      assert_equal nil, Symbol.find(str)
+      assert_equal nil, Symbol.find(str)
+      sym = str.intern
+      assert_equal str, sym.to_s
+      assert_equal sym, Symbol.find(str)
+    end;
+  end
 end

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]