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

ruby-changes:50942

From: nobu <ko1@a...>
Date: Sat, 14 Apr 2018 21:49:37 +0900 (JST)
Subject: [ruby-changes:50942] nobu:r63149 (trunk): irb/{context, workspace}.rb: use local_variable_set

nobu	2018-04-14 21:49:30 +0900 (Sat, 14 Apr 2018)

  New Revision: 63149

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=63149

  Log:
    irb/{context,workspace}.rb: use local_variable_set

  Added files:
    trunk/test/irb/test_context.rb
  Modified files:
    trunk/lib/irb/context.rb
    trunk/lib/irb/workspace.rb
Index: test/irb/test_context.rb
===================================================================
--- test/irb/test_context.rb	(nonexistent)
+++ test/irb/test_context.rb	(revision 63149)
@@ -0,0 +1,44 @@ https://github.com/ruby/ruby/blob/trunk/test/irb/test_context.rb#L1
+# frozen_string_literal: false
+require 'test/unit'
+require 'tempfile'
+require 'irb'
+require 'rubygems' if defined?(Gem)
+
+module TestIRB
+  class TestContext < Test::Unit::TestCase
+    class TestInputMethod < ::IRB::InputMethod
+      attr_reader :line, :line_no
+
+      def initialize(list = [])
+        super("test")
+        @line_no = 0
+        @line = list
+      end
+
+      def gets
+        @list[@line_no.tap {@line_no += 1}]
+      end
+
+      def eof?
+        @line_no >= @list.size
+      end
+    end
+
+    def setup
+      IRB.init_config(nil)
+      IRB.conf[:USE_READLINE] = false
+      IRB.conf[:VERBOSE] = false
+      workspace = IRB::WorkSpace.new(Object.new)
+      @context = IRB::Context.new(nil, workspace, TestInputMethod.new)
+    end
+
+    def test_last_value
+      assert_nil(@context.last_value)
+      assert_nil(@context.evaluate('_', 1))
+      obj = Object.new
+      @context.set_last_value(obj)
+      assert_same(obj, @context.last_value)
+      assert_same(obj, @context.evaluate('_', 1))
+    end
+  end
+end

Property changes on: test/irb/test_context.rb
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+LF
\ No newline at end of property
Index: lib/irb/context.rb
===================================================================
--- lib/irb/context.rb	(revision 63148)
+++ lib/irb/context.rb	(revision 63149)
@@ -262,7 +262,7 @@ module IRB https://github.com/ruby/ruby/blob/trunk/lib/irb/context.rb#L262
     # to #last_value.
     def set_last_value(value)
       @last_value = value
-      @workspace.evaluate self, "_ = IRB.CurrentContext.last_value"
+      @workspace.local_variable_set :_, value
     end
 
     # Sets the +mode+ of the prompt in this context.
Index: lib/irb/workspace.rb
===================================================================
--- lib/irb/workspace.rb	(revision 63148)
+++ lib/irb/workspace.rb	(revision 63149)
@@ -71,7 +71,7 @@ EOF https://github.com/ruby/ruby/blob/trunk/lib/irb/workspace.rb#L71
           end
         end
       end
-      eval("_=nil", @binding)
+      @binding.local_variable_set(:_, nil)
     end
 
     # The Binding of this workspace
@@ -85,6 +85,14 @@ EOF https://github.com/ruby/ruby/blob/trunk/lib/irb/workspace.rb#L85
       eval(statements, @binding, file, line)
     end
 
+    def local_variable_set(name, value)
+      @binding.local_variable_set(name, value)
+    end
+
+    def local_variable_get(name)
+      @binding.local_variable_get(name)
+    end
+
     # error message manipulator
     def filter_backtrace(bt)
       case IRB.conf[:CONTEXT_MODE]

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

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