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

ruby-changes:51135

From: nobu <ko1@a...>
Date: Sun, 6 May 2018 18:53:39 +0900 (JST)
Subject: [ruby-changes:51135] nobu:r63342 (trunk): optparse: Suppress warnings

nobu	2018-05-06 18:53:33 +0900 (Sun, 06 May 2018)

  New Revision: 63342

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

  Log:
    optparse: Suppress warnings
    
    Ruby warns "instance variable `@version` not initialized" for optparse when it receives `--version` option.
    
    `test.rb`
    
    ```ruby
    require 'optparse'
    Version = '1'
    OptionParser.new.parse(ARGV)
    ```
    
    ```
    $ ruby -w test.rb --version
    /home/pocke/.rbenv/versions/2.5.1/lib/ruby/2.5.0/optparse.rb:1168: warning: instance variable @version not initialized
    /home/pocke/.rbenv/versions/2.5.1/lib/ruby/2.5.0/optparse.rb:1175: warning: instance variable @release not initialized
    test 1
    ```
    
    This change will suppress the warnings.
    
    [Fix GH-1871]
    
    From: Masataka Pocke Kuwabara <kuwabara@p...>

  Modified files:
    trunk/lib/optparse.rb
    trunk/test/optparse/test_summary.rb
Index: test/optparse/test_summary.rb
===================================================================
--- test/optparse/test_summary.rb	(revision 63341)
+++ test/optparse/test_summary.rb	(revision 63342)
@@ -48,6 +48,8 @@ class TestOptionParser::SummaryTest < Te https://github.com/ruby/ruby/blob/trunk/test/optparse/test_summary.rb#L48
   def test_ver
     o = OptionParser.new("foo bar")
     o.program_name = "foo"
+    assert_warning('') {assert_nil(o.version)}
+    assert_warning('') {assert_nil(o.release)}
     o.version = [0, 1]
     assert_equal "foo 0.1", o.ver
     o.release = "rel"
Index: lib/optparse.rb
===================================================================
--- lib/optparse.rb	(revision 63341)
+++ lib/optparse.rb	(revision 63342)
@@ -1165,14 +1165,14 @@ XXX https://github.com/ruby/ruby/blob/trunk/lib/optparse.rb#L1165
   # Version
   #
   def version
-    @version || (defined?(::Version) && ::Version)
+    (defined?(@version) && @version) || (defined?(::Version) && ::Version)
   end
 
   #
   # Release code
   #
   def release
-    @release || (defined?(::Release) && ::Release) || (defined?(::RELEASE) && ::RELEASE)
+    (defined?(@release) && @release) || (defined?(::Release) && ::Release) || (defined?(::RELEASE) && ::RELEASE)
   end
 
   #

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

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