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

ruby-changes:24611

From: nobu <ko1@a...>
Date: Wed, 8 Aug 2012 22:46:19 +0900 (JST)
Subject: [ruby-changes:24611] nobu:r36662 (trunk): use local variables

nobu	2012-08-08 22:46:09 +0900 (Wed, 08 Aug 2012)

  New Revision: 36662

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

  Log:
    use local variables
    
    * test/ruby/test_eval.rb: use local variables instead global variables
      if possible.
    * test/ruby/test_ifunless.rb: ditto.
    * test/ruby/test_iterator.rb: ditto.
    * test/ruby/test_stringchar.rb: ditto.

  Modified files:
    trunk/test/ruby/test_eval.rb
    trunk/test/ruby/test_ifunless.rb
    trunk/test/ruby/test_iterator.rb
    trunk/test/ruby/test_stringchar.rb

Index: test/ruby/test_ifunless.rb
===================================================================
--- test/ruby/test_ifunless.rb	(revision 36661)
+++ test/ruby/test_ifunless.rb	(revision 36662)
@@ -2,13 +2,13 @@
 
 class TestIfunless < Test::Unit::TestCase
   def test_if_unless
-    $x = 'test';
-    assert(if $x == $x then true else false end)
-    $bad = false
-    unless $x == $x
-      $bad = true
+    x = 'test';
+    assert(if x == x then true else false end)
+    bad = false
+    unless x == x
+      bad = true
     end
-    assert(!$bad)
-    assert(unless $x != $x then true else false end)
+    assert(!bad)
+    assert(unless x != x then true else false end)
   end
 end
Index: test/ruby/test_iterator.rb
===================================================================
--- test/ruby/test_iterator.rb	(revision 36661)
+++ test/ruby/test_iterator.rb	(revision 36662)
@@ -25,14 +25,14 @@
   end
 
   def test_array
-    $x = [1, 2, 3, 4]
-    $y = []
+    x = [1, 2, 3, 4]
+    y = []
 
     # iterator over array
-    for i in $x
-      $y.push i
+    for i in x
+      y.push i
     end
-    assert_equal($x, $y)
+    assert_equal(x, y)
   end
 
   def tt
@@ -79,36 +79,36 @@
     assert(done)
 
     done = false
-    $bad = false
+    bad = false
     loop {
       break if done
       done = true
       next
-      $bad = true			# should not reach here
+      bad = true			# should not reach here
     }
-    assert(!$bad)
+    assert(!bad)
 
     done = false
-    $bad = false
+    bad = false
     loop {
       break if done
       done = true
       redo
-      $bad = true			# should not reach here
+      bad = true			# should not reach here
     }
-    assert(!$bad)
+    assert(!bad)
 
-    $x = []
+    x = []
     for i in 1 .. 7
-      $x.push i
+      x.push i
     end
-    assert_equal(7, $x.size)
-    assert_equal([1, 2, 3, 4, 5, 6, 7], $x)
+    assert_equal(7, x.size)
+    assert_equal([1, 2, 3, 4, 5, 6, 7], x)
   end
 
   def test_append_method_to_built_in_class
-    $x = [[1,2],[3,4],[5,6]]
-    assert_equal($x.iter_test1{|x|x}, $x.iter_test2{|x|x})
+    x = [[1,2],[3,4],[5,6]]
+    assert_equal(x.iter_test1{|x|x}, x.iter_test2{|x|x})
   end
 
   class IterTest
Index: test/ruby/test_eval.rb
===================================================================
--- test/ruby/test_eval.rb	(revision 36661)
+++ test/ruby/test_eval.rb	(revision 36662)
@@ -229,9 +229,9 @@
 
   def test_eval_orig
     assert_nil(eval(""))
-    $bad=false
-    eval 'while false; $bad = true; print "foo\n" end'
-    assert(!$bad)
+    bad=false
+    eval 'while false; bad = true; print "foo\n" end'
+    assert(!bad)
 
     assert(eval('TRUE'))
     assert(eval('true'))
@@ -254,34 +254,34 @@
     assert_equal(5, eval("i"))
     assert(eval("defined? i"))
 
-    $x = test_ev
-    assert_equal("local1", eval("local1", $x)) # normal local var
-    assert_equal("local2", eval("local2", $x)) # nested local var
-    $bad = true
+    x = test_ev
+    assert_equal("local1", eval("local1", x)) # normal local var
+    assert_equal("local2", eval("local2", x)) # nested local var
+    bad = true
     begin
       p eval("local1")
     rescue NameError		# must raise error
-      $bad = false
+      bad = false
     end
-    assert(!$bad)
+    assert(!bad)
 
     # !! use class_eval to avoid nested definition
-    self.class.class_eval %q(
+    x = self.class.class_eval %q(
       module EvTest
 	EVTEST1 = 25
 	evtest2 = 125
-	$x = binding
+	binding
       end
     )
-    assert_equal(25, eval("EVTEST1", $x))	# constant in module
-    assert_equal(125, eval("evtest2", $x))	# local var in module
-    $bad = true
+    assert_equal(25, eval("EVTEST1", x))	# constant in module
+    assert_equal(125, eval("evtest2", x))	# local var in module
+    bad = true
     begin
       eval("EVTEST1")
     rescue NameError		# must raise error
-      $bad = false
+      bad = false
     end
-    assert(!$bad)
+    assert(!bad)
 
     if false
       # Ruby 2.0 doesn't see Proc as Binding
@@ -291,10 +291,10 @@
       x = proc{proc{}}.call
       eval "i4 = 22", x
       assert_equal(22, eval("i4", x))
-      $x = []
+      t = []
       x = proc{proc{}}.call
-      eval "(0..9).each{|i5| $x[i5] = proc{i5*2}}", x
-      assert_equal(8, $x[4].call)
+      eval "(0..9).each{|i5| t[i5] = proc{i5*2}}", x
+      assert_equal(8, t[4].call)
     end
 
     x = binding
@@ -303,10 +303,10 @@
     x = proc{binding}.call
     eval "i = 22", x
     assert_equal(22, eval("i", x))
-    $x = []
+    t = []
     x = proc{binding}.call
-    eval "(0..9).each{|i5| $x[i5] = proc{i5*2}}", x
-    assert_equal(8, $x[4].call)
+    eval "(0..9).each{|i5| t[i5] = proc{i5*2}}", x
+    assert_equal(8, t[4].call)
     x = proc{binding}.call
     eval "for i6 in 1..1; j6=i6; end", x
     assert(eval("defined? i6", x))
Index: test/ruby/test_stringchar.rb
===================================================================
--- test/ruby/test_stringchar.rb	(revision 36661)
+++ test/ruby/test_stringchar.rb	(revision 36662)
@@ -30,12 +30,12 @@
     assert(/(\s+\d+){2}/ =~ " 1 2"); assert_equal(" 1 2", $&)
     assert(/(?:\s+\d+){2}/ =~ " 1 2"); assert_equal(" 1 2", $&)
 
-    $x = <<END;
+    x = <<END;
 ABCD
 ABCD
 END
-    $x.gsub!(/((.|\n)*?)B((.|\n)*?)D/m ,'\1\3')
-    assert_equal("AC\nAC\n", $x)
+    x.gsub!(/((.|\n)*?)B((.|\n)*?)D/m ,'\1\3')
+    assert_equal("AC\nAC\n", x)
 
     assert_match(/foo(?=(bar)|(baz))/, "foobar")
     assert_match(/foo(?=(bar)|(baz))/, "foobaz")
@@ -56,12 +56,12 @@
     assert_equal('-', foo * 1)
     assert_equal('', foo * 0)
 
-    $x = "a.gif"
-    assert_equal("gif", $x.sub(/.*\.([^\.]+)$/, '\1'))
-    assert_equal("b.gif", $x.sub(/.*\.([^\.]+)$/, 'b.\1'))
-    assert_equal("", $x.sub(/.*\.([^\.]+)$/, '\2'))
-    assert_equal("ab", $x.sub(/.*\.([^\.]+)$/, 'a\2b'))
-    assert_equal("<a.gif>", $x.sub(/.*\.([^\.]+)$/, '<\&>'))
+    x = "a.gif"
+    assert_equal("gif", x.sub(/.*\.([^\.]+)$/, '\1'))
+    assert_equal("b.gif", x.sub(/.*\.([^\.]+)$/, 'b.\1'))
+    assert_equal("", x.sub(/.*\.([^\.]+)$/, '\2'))
+    assert_equal("ab", x.sub(/.*\.([^\.]+)$/, 'a\2b'))
+    assert_equal("<a.gif>", x.sub(/.*\.([^\.]+)$/, '<\&>'))
   end
 
   def test_char
@@ -78,16 +78,16 @@
     assert_equal("abc", "abcc".squeeze!("a-z"))
     assert_equal("ad", "abcd".delete!("bc"))
 
-    $x = "abcdef"
-    $y = [ ?a, ?b, ?c, ?d, ?e, ?f ]
-    $bad = false
-    $x.each_byte {|i|
-      if i.chr != $y.shift
-        $bad = true
+    x = "abcdef"
+    y = [ ?a, ?b, ?c, ?d, ?e, ?f ]
+    bad = false
+    x.each_byte {|i|
+      if i.chr != y.shift
+        bad = true
         break
       end
     }
-    assert(!$bad)
+    assert(!bad)
 
     s = "a string"
     s[0..s.size]="another string"

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

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