https://bugs.ruby-lang.org/https://bugs.ruby-lang.org/favicon.ico?17113305112020-12-12T12:47:30ZRuby Issue Tracking SystemRuby master - Bug #17387: About Warning.warn compatibility in Ruby 3.0.0https://bugs.ruby-lang.org/issues/17387?journal_id=891952020-12-12T12:47:30ZEregon (Benoit Daloze)
<ul><li><strong>Related to</strong> <i><a class="issue tracker-2 status-5 priority-4 priority-default closed" href="/issues/17122">Feature #17122</a>: Add category to Warning#warn</i> added</li></ul> Ruby master - Bug #17387: About Warning.warn compatibility in Ruby 3.0.0https://bugs.ruby-lang.org/issues/17387?journal_id=891962020-12-12T12:49:31ZEregon (Benoit Daloze)
<ul></ul><p>The English documentation seems fine:<br>
<a href="https://docs.ruby-lang.org/en/master/Warning.html#method-i-warn" class="external">https://docs.ruby-lang.org/en/master/Warning.html#method-i-warn</a></p>
<p>So I guess the Japanese docs should be updated.</p> Ruby master - Bug #17387: About Warning.warn compatibility in Ruby 3.0.0https://bugs.ruby-lang.org/issues/17387?journal_id=891982020-12-12T21:51:31Zjnchito (Junichi Ito)
<ul></ul><p>Eregon (Benoit Daloze) wrote in <a href="#note-2">#note-2</a>:</p>
<blockquote>
<p>The English documentation seems fine:<br>
<a href="https://docs.ruby-lang.org/en/master/Warning.html#method-i-warn" class="external">https://docs.ruby-lang.org/en/master/Warning.html#method-i-warn</a></p>
<p>So I guess the Japanese docs should be updated.</p>
</blockquote>
<p>Thank you for information. I noticed the first parameter can be string and array according to the method signature.</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">module</span> <span class="nn">Warning</span>
<span class="k">def</span> <span class="nc">self</span><span class="o">.</span><span class="nf">warn</span><span class="p">(</span><span class="n">message</span><span class="p">)</span>
<span class="nb">p</span> <span class="n">message</span> <span class="c1">#=> "my warning\n"</span>
<span class="k">super</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="nb">warn</span> <span class="s2">"my warning"</span>
</code></pre>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">module</span> <span class="nn">Warning</span>
<span class="k">def</span> <span class="nc">self</span><span class="o">.</span><span class="nf">warn</span><span class="p">(</span><span class="o">*</span><span class="n">message</span><span class="p">)</span>
<span class="nb">p</span> <span class="n">message</span> <span class="c1">#=> ["my warning\n"]</span>
<span class="k">super</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="nb">warn</span> <span class="s2">"my warning"</span>
</code></pre>
<p>The both can run in Ruby 2.7, but the latter won't run in Ruby 3.0.0 with the message below:</p>
<pre><code>$ ruby ~/Desktop/test.rb
["my warning\n", {:category=>nil}]
/Users/jnito/Desktop/test.rb:4:in `warn': wrong number of arguments (given 2, expected 1) (ArgumentError)
from /Users/jnito/Desktop/test.rb:4:in `warn'
from <internal:warning>:43:in `warn'
from /Users/jnito/Desktop/test.rb:7:in `<main>'
</code></pre>
<p>Therefore, the compatibility is broken (the parameter <code>message</code> should be <code>["my warning\n"]</code>). Is it a bug or not?</p> Ruby master - Bug #17387: About Warning.warn compatibility in Ruby 3.0.0https://bugs.ruby-lang.org/issues/17387?journal_id=891992020-12-13T00:26:01Zjeremyevans0 (Jeremy Evans)merch-redmine@jeremyevans.net
<ul></ul><p>jnchito (Junichi Ito) wrote in <a href="#note-3">#note-3</a>:</p>
<blockquote>
<p>Therefore, the compatibility is broken (the parameter <code>message</code> should be <code>["my warning\n"]</code>). Is it a bug or not?</p>
</blockquote>
<p>It is only a bug in the Japanese documentation. Unlike <code>Kernel#warn</code>, <code>Warning.warn</code> in Ruby 2.7 only accepted a single string:</p>
<pre><code class="c syntaxhl" data-language="c"><span class="n">rb_define_method</span><span class="p">(</span><span class="n">rb_mWarning</span><span class="p">,</span> <span class="s">"warn"</span><span class="p">,</span> <span class="n">rb_warning_s_warn</span><span class="p">,</span> <span class="mi">1</span><span class="p">);</span>
<span class="n">rb_extend_object</span><span class="p">(</span><span class="n">rb_mWarning</span><span class="p">,</span> <span class="n">rb_mWarning</span><span class="p">);</span>
</code></pre>
<p>As keywords were added to the method in Ruby 3, you can still use the keyword splat when overriding, as long as your override also uses keyword arguments:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">module</span> <span class="nn">Warning</span>
<span class="k">def</span> <span class="nc">self</span><span class="o">.</span><span class="nf">warn</span><span class="p">(</span><span class="o">*</span><span class="n">message</span><span class="p">,</span> <span class="o">**</span><span class="p">)</span>
<span class="nb">p</span> <span class="n">message</span> <span class="c1">#=> ["my warning\n"]</span>
<span class="k">super</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="nb">warn</span> <span class="s2">"my warning"</span>
</code></pre>
<p>This could be considered a breakage of backwards compatibility, but that is not specific to this case, it is for any method where you add keywords arguments. If you have a method that doesn't accept keyword arguments and you want to allow adding keyword arguments to the method later (safe keyword extension), you have to use the <code>**nil</code> syntax (no keywords accepted). Of course, if you do this, trying to pass a final positional hash using keyword syntax will no longer work.</p> Ruby master - Bug #17387: About Warning.warn compatibility in Ruby 3.0.0https://bugs.ruby-lang.org/issues/17387?journal_id=892002020-12-13T01:30:59Zjnchito (Junichi Ito)
<ul></ul><p>jeremyevans0 (Jeremy Evans) wrote in <a href="#note-4">#note-4</a>:</p>
<blockquote>
<p>jnchito (Junichi Ito) wrote in <a href="#note-3">#note-3</a>:</p>
<blockquote>
<p>Therefore, the compatibility is broken (the parameter <code>message</code> should be <code>["my warning\n"]</code>). Is it a bug or not?</p>
</blockquote>
<p>It is only a bug in the Japanese documentation. Unlike <code>Kernel#warn</code>, <code>Warning.warn</code> in Ruby 2.7 only accepted a single string:</p>
</blockquote>
<p>Thank you for your comment. As for this issue, I should fix the Japanese document, so I submitted PR: <a href="https://github.com/rurema/doctree/pull/2411" class="external">https://github.com/rurema/doctree/pull/2411</a></p> Ruby master - Bug #17387: About Warning.warn compatibility in Ruby 3.0.0https://bugs.ruby-lang.org/issues/17387?journal_id=894882020-12-24T02:56:51Zmame (Yusuke Endoh)mame@ruby-lang.org
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Closed</i></li></ul><p>The Japanese doc has been fixed. This is actually an incompatibility, but I believe it is a minor issue. Thank you for the report. Closing.</p>