https://bugs.ruby-lang.org/https://bugs.ruby-lang.org/favicon.ico?17113305112015-08-29T23:05:56ZRuby Issue Tracking SystemRuby master - Bug #11495: [Documentation] Please improve documentation for Regexp.new() and clarify the 3 argument callhttps://bugs.ruby-lang.org/issues/11495?journal_id=540382015-08-29T23:05:56Zshevegen (Robert A. Heiler)shevegen@gmail.com
<ul></ul><p>The current examples are:</p>
<pre><code>r1 = Regexp.new('^a-z+:\s+\w+') #=> /^a-z+:\s+\w+/
r2 = Regexp.new('cat', true) #=> /cat/i
r3 = Regexp.new(r2) #=> /cat/i
r4 = Regexp.new('dog', Regexp::EXTENDED | Regexp::IGNORECASE) #=> /dog/ix
</code></pre>
<p>As you can see, we have examples for one argument, and two arguments given<br>
but not for three arguments.</p>
<p>Thanks!</p> Ruby master - Bug #11495: [Documentation] Please improve documentation for Regexp.new() and clarify the 3 argument callhttps://bugs.ruby-lang.org/issues/11495?journal_id=544192015-10-12T00:10:00Zdpulliam (Dylan Pulliam)dylan.pulliam@gmail.com
<ul><li><strong>File</strong> <a href="/attachments/5519">0001-Improve-documentation-for-Regexp.new.patch</a> <a class="icon-only icon-download" title="Download" href="/attachments/download/5519/0001-Improve-documentation-for-Regexp.new.patch">0001-Improve-documentation-for-Regexp.new.patch</a> added</li></ul><p>Robert A. Heiler wrote:</p>
<blockquote>
<p>The current examples are:</p>
<pre><code>r1 = Regexp.new('^a-z+:\s+\w+') #=> /^a-z+:\s+\w+/
r2 = Regexp.new('cat', true) #=> /cat/i
r3 = Regexp.new(r2) #=> /cat/i
r4 = Regexp.new('dog', Regexp::EXTENDED | Regexp::IGNORECASE) #=> /dog/ix
</code></pre>
<p>As you can see, we have examples for one argument, and two arguments given<br>
but not for three arguments.</p>
<p>Thanks!</p>
</blockquote>
<p>Hey Robert,</p>
<p>TL;DR</p>
<p>$KCODE is deprecated and does not affect the use of Regexp at this point. I have a patch ready to go that removes [, kcode] from the doc. That being said I would be happy to modify the patch and just submit an example of using Regexp with 3 parameters.</p>
<p>r5 = Regexp.new('fish', Regexp::IGNORECASE, 'utf8')</p>
<p>The Good Stuff</p>
<p>The kcode parameter in new(string, [options [, kcode]]) or compile(string, [options [, kcode]]) is used to set the global $KCODE variable which basically sets the encoding to be used. This could be UTF-8, Unicode, or any other valid encoding etc.</p>
<p>I believe the example you are looking for would be the following:</p>
<p>r5 = Regexp.new('fish', Regexp::IGNORECASE, 'utf8')</p>
<p>This example is not dependent on the [option] Regexp::IGNORECASE. Any other accepted option should still work with the above example.</p>
<p>However, $KCODE was deprecated in Ruby 1.9 so you will find that setting the encoding does not do anything and you will receive a warning in most cases. You can see this here:</p>
<p>ruby regexp_example.rb<br>
regexp_example.rb:3: warning: encoding option is ignored - utf8</p>
<p>or</p>
<p>irb(main):001:0> Regexp.new('fish', Regexp::IGNORECASE, 'utf8')<br>
(irb):1: warning: encoding option is ignored - utf8<br>
=> /fish/i</p>
<p>However setting the encoding at the console does not provide a warning:</p>
<p>ruby -KU #=> Runs Ruby with UTF-8 encoding</p>
<p>I went ahead and prepared a patch for the documentation. You should be able to find it attached to this comment. I think the best course of action would be to remove [, kcode] since you cannot pass any option that will actually change the outcome. The internal C code will default to ‘n’ or ‘N’ which you can pass in without warning but basically just says “don’t specify encoding just treat as binary string”. That being said since it was deprecated I would be happy to modify the patch and just add the example for now.</p>
<p>If you would like more information on regular expressions and kcodes in ruby<br>
go ahead and checkout the following links. They helped a lot when I was<br>
researching this topic.</p>
<p><a href="http://nuclearsquid.com/writings/ruby-1-9-what-s-new-what-s-changed/" class="external">http://nuclearsquid.com/writings/ruby-1-9-what-s-new-what-s-changed/</a><br>
<a href="http://graysoftinc.com/character-encodings/the-kcode-variable-and-jcode-library" class="external">http://graysoftinc.com/character-encodings/the-kcode-variable-and-jcode-library</a><br>
<a href="https://www.ruby-forum.com/topic/178589" class="external">https://www.ruby-forum.com/topic/178589</a><br>
<a href="http://yehudakatz.com/2010/05/05/ruby-1-9-encodings-a-primer-and-the-solution-for-rails/" class="external">http://yehudakatz.com/2010/05/05/ruby-1-9-encodings-a-primer-and-the-solution-for-rails/</a></p> Ruby master - Bug #11495: [Documentation] Please improve documentation for Regexp.new() and clarify the 3 argument callhttps://bugs.ruby-lang.org/issues/11495?journal_id=560952016-01-14T17:44:59Zdpulliam (Dylan Pulliam)dylan.pulliam@gmail.com
<ul><li><strong>Tracker</strong> changed from <i>Misc</i> to <i>Bug</i></li><li><strong>Assignee</strong> set to <i>zzak (zzak _)</i></li><li><strong>Backport</strong> set to <i>2.0.0: UNKNOWN, 2.1: UNKNOWN, 2.2: UNKNOWN, 2.3: UNKNOWN</i></li></ul> Ruby master - Bug #11495: [Documentation] Please improve documentation for Regexp.new() and clarify the 3 argument callhttps://bugs.ruby-lang.org/issues/11495?journal_id=569322016-02-09T04:54:30Zzzak (zzak _)
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Closed</i></li></ul><p>Applied in changeset r53784.</p>
<hr>
<ul>
<li>re.c: Remove deprecated kcode argument from Regexp.new and compile<br>
patch provided by Dylan Pulliam [Bug <a class="issue tracker-1 status-5 priority-4 priority-default closed" title="Bug: [Documentation] Please improve documentation for Regexp.new() and clarify the 3 argument call (Closed)" href="https://bugs.ruby-lang.org/issues/11495">#11495</a>]</li>
</ul> Ruby master - Bug #11495: [Documentation] Please improve documentation for Regexp.new() and clarify the 3 argument callhttps://bugs.ruby-lang.org/issues/11495?journal_id=570932016-02-23T08:53:13Zusa (Usaku NAKAMURA)usa@garbagecollect.jp
<ul><li><strong>Backport</strong> changed from <i>2.0.0: UNKNOWN, 2.1: UNKNOWN, 2.2: UNKNOWN, 2.3: UNKNOWN</i> to <i>2.1: REQUIRED, 2.2: REQUIRED, 2.3: REQUIRED</i></li></ul> Ruby master - Bug #11495: [Documentation] Please improve documentation for Regexp.new() and clarify the 3 argument callhttps://bugs.ruby-lang.org/issues/11495?journal_id=571382016-02-25T11:00:05Zusa (Usaku NAKAMURA)usa@garbagecollect.jp
<ul><li><strong>Backport</strong> changed from <i>2.1: REQUIRED, 2.2: REQUIRED, 2.3: REQUIRED</i> to <i>2.1: DONE, 2.2: REQUIRED, 2.3: REQUIRED</i></li></ul><p>ruby_2_1 r53937 merged revision(s) 53784.</p> Ruby master - Bug #11495: [Documentation] Please improve documentation for Regexp.new() and clarify the 3 argument callhttps://bugs.ruby-lang.org/issues/11495?journal_id=573812016-03-09T15:21:18Znagachika (Tomoyuki Chikanaga)nagachika00@gmail.com
<ul><li><strong>Backport</strong> changed from <i>2.1: DONE, 2.2: REQUIRED, 2.3: REQUIRED</i> to <i>2.1: DONE, 2.2: DONE, 2.3: REQUIRED</i></li></ul><p>r51006 and r53784 were backported into <code>ruby_2_2</code> branch at r54071.</p> Ruby master - Bug #11495: [Documentation] Please improve documentation for Regexp.new() and clarify the 3 argument callhttps://bugs.ruby-lang.org/issues/11495?journal_id=577832016-03-29T05:57:54Znaruse (Yui NARUSE)naruse@airemix.jp
<ul><li><strong>Backport</strong> changed from <i>2.1: DONE, 2.2: DONE, 2.3: REQUIRED</i> to <i>2.1: DONE, 2.2: DONE, 2.3: DONE</i></li></ul><p>ruby_2_3 r54361 merged revision(s) 53784.</p>