Ruby Issue Tracking System: Issueshttps://bugs.ruby-lang.org/https://bugs.ruby-lang.org/favicon.ico?17113305112015-10-15T19:36:46ZRuby Issue Tracking System
Redmine Ruby master - Feature #11597 (Open): Add Linux-specific setfsuid(2)/setfsgid(2)https://bugs.ruby-lang.org/issues/115972015-10-15T19:36:46Zvihai (Daniele Orlandi)daniele@orlandi.com
<p>I would be helpful if you add a binding to the Linux-specific syscalls</p>
<p>setfsuid(2)<br>
setfsgid(2)</p>
<p>Their purpose is to change the uid/gid with which filesystem access control is performed. Their main usage is simulating fs access on behalf of other users.</p>
<p>Differently from setuid/setreuid/etc... thess calls have thread-local effect, thus they may be useful in solving some problems when needing to temporarily drop privileges in multi-threaded applications.</p> Ruby master - Feature #11583 (Open): Add File#unlinkhttps://bugs.ruby-lang.org/issues/115832015-10-12T04:32:48Zksss (Yuki Kurihara)co000ri@gmail.com
<p>I propose new method <strong>File#unlink</strong>.</p>
<p><strong>File#unlink</strong> is same as <strong>Fille.unlink</strong> like this.</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">class</span> <span class="nc">File</span>
<span class="k">def</span> <span class="nf">unlink</span>
<span class="no">File</span><span class="p">.</span><span class="nf">unlink</span><span class="p">(</span><span class="n">path</span><span class="p">)</span>
<span class="k">end</span>
<span class="k">end</span>
</code></pre>
<p>It is more objective.<br>
And will be able to write duck typeing with <strong>Tempfile#unlink</strong></p>
<pre><code class="diff syntaxhl" data-language="diff"><span class="gd">- Tempfile.open do |f|
</span><span class="gi">+ Tempfile.create do |f|
</span> f.unlink # Not raise an error
<span class="p">end
</span></code></pre> Ruby master - Feature #11577 (Open): Add encodeURIComponent compatible API for URIhttps://bugs.ruby-lang.org/issues/115772015-10-09T13:40:10Znaruse (Yui NARUSE)naruse@airemix.jp
<p>How about adding encodeURIComponent/decodeURIComponent compatible API?</p>
<p>There's already have some methods:</p>
<ul>
<li>URI.escape: context aware but deprecated.</li>
<li>URIencode_www_form: application/x-www-form-urlencoded, which encodes spaces into '+'</li>
<li>URIencode_www_form_component: above component</li>
</ul>
<p>So it doesn't have non-form URI escape method.</p>
<pre><code class="diff syntaxhl" data-language="diff"><span class="gh">diff --git a/lib/uri/common.rb b/lib/uri/common.rb
index 1444ae8..0017ae3 100644
</span><span class="gd">--- a/lib/uri/common.rb
</span><span class="gi">+++ b/lib/uri/common.rb
</span><span class="p">@@ -1,4 +1,5 @@</span>
<span class="gd">-#--
</span><span class="gi">+#
+# -*- frozen-string-literal: true -*-
</span> # = uri/common.rb
#
# Author:: Akira Yamada <akira@ruby-lang.org>
<span class="p">@@ -329,27 +330,72 @@</span> module URI
DEFAULT_PARSER.make_regexp(schemes)
end
<span class="gi">+ TBLENCURICOMP_ = {} # :nodoc:
</span> TBLENCWWWCOMP_ = {} # :nodoc:
<span class="gd">- 256.times do |i|
- TBLENCWWWCOMP_[i.chr] = '%%%02X' % i
- end
- TBLENCWWWCOMP_[' '] = '+'
- TBLENCWWWCOMP_.freeze
</span><span class="gi">+ TBLDECURICOMP_ = {} # :nodoc:
</span> TBLDECWWWCOMP_ = {} # :nodoc:
256.times do |i|
h, l = i>>4, i&15
<span class="gd">- TBLDECWWWCOMP_['%%%X%X' % [h, l]] = i.chr
- TBLDECWWWCOMP_['%%%x%X' % [h, l]] = i.chr
- TBLDECWWWCOMP_['%%%X%x' % [h, l]] = i.chr
- TBLDECWWWCOMP_['%%%x%x' % [h, l]] = i.chr
</span><span class="gi">+ c = i.chr.freeze
+ k = sprintf('%%%X%X', h, l).freeze
+ TBLENCURICOMP_[c] = TBLENCWWWCOMP_[c] = k
+ TBLDECURICOMP_[k] = TBLDECWWWCOMP_[k] = c
+ k = sprintf('%%%x%X', h, l).freeze
+ TBLDECURICOMP_[k] = TBLDECWWWCOMP_[k] = c
+ k = sprintf('%%%X%x', h, l).freeze
+ TBLDECURICOMP_[k] = TBLDECWWWCOMP_[k] = c
+ k = sprintf('%%%x%x', h, l).freeze
+ TBLDECURICOMP_[k] = TBLDECWWWCOMP_[k] = c
</span> end
<span class="gi">+ TBLENCWWWCOMP_[' '] = '+'
+ TBLENCWWWCOMP_.freeze
+ TBLENCURICOMP_.freeze
</span> TBLDECWWWCOMP_['+'] = ' '
TBLDECWWWCOMP_.freeze
<span class="gi">+ TBLDECURICOMP_.freeze
+
+ # Encode given +str+ to URL-encoded form data.
+ #
+ # This method doesn't convert *, -, ., 0-9, A-Z, _, a-z, but does convert SP
+ # (ASCII space) to + and converts others to %XX.
+ #
+ # If +enc+ is given, convert +str+ to the encoding before percent encoding.
+ #
+ # This is an implementation of
+ # http://www.ecma-international.org/ecma-262/6.0/#sec-encodeuricomponent-uricomponent
+ #
+ # See URI.encode_www_form_component, URI.encode_www_form
+ def self.encode_component(str)
+ str = str.to_s.dup
+ if !str.ascii_only?
+ enc = str.encoding
+ if enc != Encoding::ASCII_8BIT && enc != Encoding::UTF_8
+ str.encode!(Encoding::UTF_8, invalid: :replace, undef: :replace)
+ end
+ str.force_encoding(Encoding::ASCII_8BIT)
+ end
+ str.gsub!(/[^\-_.!~*'()0-9A-Za-z]/, TBLENCURICOMP_)
+ str.force_encoding(Encoding::US_ASCII)
+ end
+
+ # Decode given +str+ of URL-encoded form data.
+ #
+ #
+ # This doesn't decodes + to SP.
+ #
+ # This is an implementation of
+ # http://www.ecma-international.org/ecma-262/6.0/#sec-decodeuricomponent-encodeduricomponent
+ #
+ # See URI.decode_www_form_component, URI.decode_www_form
+ def self.decode_component(str, enc=Encoding::UTF_8)
+ raise ArgumentError, "invalid %-encoding (#{str})" if /%(?!\h\h)/ =~ str
+ str.b.gsub(/%\h\h/, TBLDECURICOMP_).force_encoding(enc)
+ end
</span>
HTML5ASCIIINCOMPAT = defined? Encoding::UTF_7 ? [Encoding::UTF_7, Encoding::UTF_16BE, Encoding::UTF_16LE,
Encoding::UTF_32BE, Encoding::UTF_32LE] : [] # :nodoc:
<span class="gd">- # Encode given +str+ to URL-encoded form data.
</span><span class="gi">+ # Encode given +str+ in application/x-www-form-urlencoded format.
</span> #
# This method doesn't convert *, -, ., 0-9, A-Z, _, a-z, but does convert SP
# (ASCII space) to + and converts others to %XX.
<span class="p">@@ -373,7 +419,7 @@</span> module URI
str.force_encoding(Encoding::US_ASCII)
end
<span class="gd">- # Decode given +str+ of URL-encoded form data.
</span><span class="gi">+ # Decode given +str+ in application/x-www-form-urlencoded format.
</span> #
# This decodes + to SP.
#
<span class="p">@@ -457,7 +503,7 @@</span> module URI
if isindex
if sep.empty?
val = key
<span class="gd">- key = ''
</span><span class="gi">+ key = String.new
</span> end
isindex = false
end
<span class="p">@@ -471,7 +517,7 @@</span> module URI
if val
val.gsub!(/\+|%\h\h/, TBLDECWWWCOMP_)
else
<span class="gd">- val = ''
</span><span class="gi">+ val = String.new
</span> end
ary << [key, val]
<span class="gh">diff --git a/test/uri/test_common.rb b/test/uri/test_common.rb
index 5620415..c6b5633 100644
</span><span class="gd">--- a/test/uri/test_common.rb
</span><span class="gi">+++ b/test/uri/test_common.rb
</span><span class="p">@@ -54,6 +54,34 @@</span> class TestCommon < Test::Unit::TestCase
assert_raise(NoMethodError) { Object.new.URI("http://www.ruby-lang.org/") }
end
<span class="gi">+ def test_encode_component
+ assert_equal("%00%20!%22%23%24%25%26'()*%2B%2C-.%2F09%3A%3B%3C%3D%3E%3F%40" \
+ "AZ%5B%5C%5D%5E_%60az%7B%7C%7D~",
+ URI.encode_component("\x00 !\"\#$%&'()*+,-./09:;<=>?@AZ[\\]^_`az{|}~"))
+ assert_equal("%E6%9F%8A", URI.encode_component(
+ "\x95\x41".force_encoding(Encoding::Shift_JIS)))
+ assert_equal("%E3%81%82", URI.encode_component(
+ "\x30\x42".force_encoding(Encoding::UTF_16BE)))
+ assert_equal("%E3%81%82", URI.encode_component(
+ "\e$B$\"\e(B".force_encoding(Encoding::ISO_2022_JP)))
+ end
+
+ def test_decode_component
+ assert_equal(" +!\"\#$%&'()*+,-./09:;<=>?@AZ[\\]^_`az{|}~",
+ URI.decode_component(
+ "%20+%21%22%23%24%25%26%27%28%29*%2B%2C-.%2F09%3A%3B%3C%3D%3E%3F%40" \
+ "AZ%5B%5C%5D%5E_%60az%7B%7C%7D%7E"))
+ assert_equal("\xA1\xA2".force_encoding(Encoding::EUC_JP),
+ URI.decode_component("%A1%A2", "EUC-JP"))
+ assert_equal("\xE3\x81\x82\xE3\x81\x82".force_encoding("UTF-8"),
+ URI.decode_component("\xE3\x81\x82%E3%81%82".force_encoding("UTF-8")))
+
+ assert_raise(ArgumentError){URI.decode_component("%")}
+ assert_raise(ArgumentError){URI.decode_component("%a")}
+ assert_raise(ArgumentError){URI.decode_component("x%a_")}
+ assert_nothing_raised(ArgumentError){URI.decode_component("x"*(1024*1024))}
+ end
+
</span> def test_encode_www_form_component
assert_equal("%00+%21%22%23%24%25%26%27%28%29*%2B%2C-.%2F09%3A%3B%3C%3D%3E%3F%40" \
"AZ%5B%5C%5D%5E_%60az%7B%7C%7D%7E",
</code></pre> Ruby master - Misc #11570 (Open): Clarify autoload chaining behavior https://bugs.ruby-lang.org/issues/115702015-10-06T16:52:25Zmwpastore (Mike Pastore)mike@oobak.org
<p>I've discovered a discrepancy between how MRI 2.1.7 and 2.2.3 handle autoload "chaining" (which I'll describe below) cf. RBX 2.5.8. I opened <a href="https://github.com/rubinius/rubinius/issues/3513" class="external">an issue</a> with them but the lead contributor of Rubinius is pushing back on me to clarify the expected behavior with you guys. Any guidance you can provide would be appreciated.</p>
<p>Essentially:</p>
<ul>
<li>File A autoloads <code>:Foo</code> from file B, and attempts to invoke methods on class <code>Foo</code>.</li>
<li>File B autoloads <code>:Foo</code> from file C, and attempts to reopen class <code>Foo</code> in order to define additional methods and attributes.</li>
<li>File C defines the base <code>Foo</code> class.</li>
</ul>
<p>In MRI 2.1.7 and 2.2.3, file A can see the methods defined in the base class defined in file C, as well as the extended methods and attributes added in file B. Both autoloads fire in the expected order and the composite class is computed and made available to the caller.</p>
<p>In RBX 2.5.8, file A can only see the extended methods and attributes defined in file B. Only the first autoload fires and the base class definition is never loaded or used.</p>
<p>Which is the correct behavior?</p> Ruby master - Feature #11507 (Open): Net::HTTP should use TCP_CORK or TCP_NOPUSH to avoid fragmen...https://bugs.ruby-lang.org/issues/115072015-09-04T09:47:25Zcanatella (Damien Merenne)dam@cosinux.org
<p>I discovered while implementing a soap client against a badly implemented http server that for sending a 723 bytes, Net::HTTP sends two packets:</p>
<pre><code>Frame 185: 579 bytes on wire (4632 bits), 579 bytes captured (4632 bits) on interface 0
Point-to-Point Protocol
Internet Protocol Version 4, Src: 127.0.0.1 (127.0.0.1), Dst: 127.0.0.1 (127.0.0.1)
Transmission Control Protocol, Src Port: 63525 (63525), Dst Port: 7001 (7001), Seq: 210, Ack: 1, Len: 523
[2 Reassembled TCP Segments (732 bytes): #183(209), #185(523)]
[Frame: 183, payload: 0-208 (209 bytes)]
[Frame: 185, payload: 209-731 (523 bytes)]
[Segment count: 2]
[Reassembled TCP length: 732]
[Reassembled TCP Data: 504f5354202f20485454502f312e310d0a534f4150416374...]
Hypertext Transfer Protocol
POST / HTTP/1.1\r\n
SOAPAction: ""\r\n
Content-Type: text/xml;charset=UTF-8\r\n
Content-Length: 523\r\n
Accept-Encoding: gzip;q=1.0,deflate;q=0.6,identity;q=0.3\r\n
Accept: */*\r\n
User-Agent: Ruby\r\n
Host: 172.16.129.10:7001\r\n
\r\n
[Full request URI: http://127.0.0.1:7001/]
[HTTP request 1/1]
[Response in frame: 187]
eXtensible Markup Language
</code></pre>
<p>So Net::HTTP client performance could be improved by using the TCP_CORK option on Linux and TCP_NOPUSH option on BSD's.</p>
<p>I implemented at the time an embedded tcp server on a relatively slow MIPS processor under Linux and using TCP_CORK greatly improved the throughput of the server, so it might also help here. I suppose it also depends on the network hardware.</p>
<p>Implementing this simply means (on linux at least) calling</p>
<pre><code>int state = 1;
setsockopt(fd, IPPROTO_TCP, TCP_CORK, &state, sizeof(state));
</code></pre>
<p>before starting to write the request and</p>
<pre><code>int state = 0;
setsockopt(fd, IPPROTO_TCP, TCP_CORK, &state, sizeof(state));
</code></pre>
<p>after when it's written. It will prevent the kernel to send anything until the request is completely written. From what I see, the BasicSocket::setsockopt supports TCP_CORK so that should be quite easy. Unfortunatly I do not have time to implement that right now but I hope I'll be able to implement it and do some tests soon.</p> Ruby master - Feature #11475 (Open): AST transformshttps://bugs.ruby-lang.org/issues/114752015-08-21T09:01:08ZTxus (Josep M. Bach)
<p>Even though Ruby is in many ways an acceptable Lisp, extending the language itself is a hard endeavour. By using reflection/meta-programming and taking advantage of its rich grammar, it's certainly possible to bend the language in extreme ways (believe me, I've done it), but there are a couple of serious limitations:</p>
<ul>
<li>Everything must be done at run-time, incurring in necessary overhead, sometimes unacceptably so.</li>
<li>The resulting code must be valid Ruby not only syntactically, but also semantically. That forbids the introduction of new semantic constructs by users themselves.</li>
</ul>
<p>Now, some people might argue in favour of a special DSL for compile-time macro-building, possibly augmenting the grammar and introduce all sorts of new tricks to do that. But I think that wouldn't be the most general solution, nor the most desirable. The alternative: a simple, object-oriented AST transforms API.</p>
<p>By building an AST transforms API and making it available to users, I argue that we could kill many birds with one stone:</p>
<ul>
<li>A rich macro system (or many!) could be implemented as a simple library, with no changes to core ruby.</li>
<li>Some micro-optimizations could be implemented as a library, which may find their way into core ruby later.</li>
<li>Experimental, new semantic constructs could be made available to users through simple libraries, and when time has proven their usefulness, maybe they could be implemented natively, if needed.</li>
</ul>
<p>I'd be up for the task if there is enough interest -- what are people's thoughts on this?</p> Ruby master - Feature #11429 (Open): Local variable assignment via regex === https://bugs.ruby-lang.org/issues/114292015-08-10T20:42:37Zseanlinsley (Sean Linsley)
<p>Currently, this assigns the <code>username</code> variable:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="sr">/(?<username>.*)@.*\..*/</span> <span class="o">=~</span> <span class="s2">"seanlinsley@example.com"</span>
</code></pre>
<p>But this does not:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="sr">/(?<username>.*)@.*\..*/</span> <span class="o">===</span> <span class="s2">"seanlinsley@example.com"</span>
</code></pre>
<p>If it did, it would be possible to set variables during a case statement, which was my original goal:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">case</span> <span class="s2">"seanlinsley@example.com"</span>
<span class="k">when</span> <span class="sr">/(?<username>.*)@.*\..*/</span>
<span class="nb">puts</span> <span class="s2">"Username is: </span><span class="si">#{</span><span class="n">username</span><span class="si">}</span><span class="s2">"</span>
<span class="k">end</span>
</code></pre>
<p>I would be happy to implement this, if it's a desired feature.</p> Ruby master - Feature #11415 (Open): autoload with a Prochttps://bugs.ruby-lang.org/issues/114152015-08-05T02:28:20Zmatthewd (Matthew Draper)matthew@trebex.net
<p><code>autoload</code> currently allows you to run arbitrary code when a constant is referenced, as long as that code is written to a file.</p>
<p>I propose extending <code>autoload</code> to also accept a Proc in place of the string filename.</p>
<p>Usage:</p>
<pre><code>autoload :Foo, -> { ::Object.const_set :Foo, 42 }
autoload :Bar, -> { require "bar_1"; require "bar_2" }
autoload :Baz, -> { with_special_lock { require "baz" } }
</code></pre>
<p>No built-in concurrency protection is provided: if a second thread encounters a constant reference before the first one sets the constant, the second thread will also enter the Proc.</p>
<p>Under simple usage, the proc will still call require at some point -- then require's locking will take effect. A more advanced user can apply their own locking.</p>
<p>I believe this is sufficient to allow the Rails dependency loader to use autoload instead of const_missing, and fix the "nested constants" problem.</p>
<p>The implementation is quite small, and it doesn't seem to make the dangers of autoload any worse than they already are. Hopefully, it can provide an easier place for users to experiment in ways to save autoload by making it more generally safe.</p>
<p><em>Alternatives</em></p>
<p>For Rails's purposes, nested constants could also be solved by a <code>missing_constant</code> (or similar) macro:</p>
<pre><code>module Foo; end
module Bar; end
Foo::Bar # => returns ::Bar
module Foo; missing_constant :Bar; end
module Bar
Foo::Bar # => invokes Foo.const_missing(:Bar)
</code></pre>
<p>More directly, instead of passing a Proc to autoload, a caller can write the code to a tempfile, and then specify that tempfile's path.</p> Ruby master - Misc #11355 (Open): Exceptions inheriting from Timeout::Error should behave the sam...https://bugs.ruby-lang.org/issues/113552015-07-15T16:59:01Zastratto (Stefano Tortarolo)stefano.tortarolo@gmail.com
<p>Bug <a class="issue tracker-1 status-6 priority-4 priority-default closed" title="Bug: "rescue Exception" rescues Timeout::ExitException (Rejected)" href="https://bugs.ruby-lang.org/issues/8730">#8730</a> addressed a common issue when using Timeout#timeout [*], but I think that the current behaviour is at the very least surprising.</p>
<p>Right now, exceptions provided to Timeout#timeout are rescuable from the inner block and that applies to Timeout::Error too.<br>
The confusing aspect is that there's no way to provide a custom exception that inherits from Timeout::Error and make it not rescuable by the inner block (i.e., <a class="issue tracker-1 status-2 priority-4 priority-default" title="Bug: Logger traps all exceptions; breaks Timeout (Assigned)" href="https://bugs.ruby-lang.org/issues/9115">#9115</a>).</p>
<p>Basically what I would expect is a way to provide a custom exception that's treated calling #catch on it in Timeout#timeout</p>
<pre><code># This could be applied to every exception that inherits from Timeout::Error
# but we need a different interface to provide it, in order to maintain the behaviour that a provided exception is rescuable
bt = Error.catch(message, &bl)
</code></pre>
<p>I'm filing this as Misc and not Bug exactly because reading the code it's expected behaviour, so I'm mainly trying to foster a conversation about whether there's a nice way to support both scenarios.</p>
<p>[*] I don't speak Japanese and I cannot fully trust Google Translate, so forgive me if I lost some fundamental concepts in that thread.</p> Ruby master - Feature #11347 (Open): Errors with cause not reported properly to consolehttps://bugs.ruby-lang.org/issues/113472015-07-13T09:36:32Zakostadinov (Aleksandar Kostadinov)akostadinov@gmail.com
<p>Hello, errors with <code>cause</code> are an important and very useful feature for debugging. It seems though default reporting is not showing the <code>cause</code> and nested causes to user. Here's a test file:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">raise</span> <span class="s2">"GAHGAH"</span> <span class="k">rescue</span> <span class="k">raise</span> <span class="s2">"error with cause"</span> <span class="k">rescue</span> <span class="n">e</span><span class="o">=</span><span class="vg">$!</span>
<span class="nb">puts</span> <span class="s2">"Error: </span><span class="si">#{</span><span class="n">e</span><span class="si">}</span><span class="s2">"</span>
<span class="nb">puts</span> <span class="s2">"Cause: </span><span class="si">#{</span><span class="n">e</span><span class="p">.</span><span class="nf">cause</span><span class="si">}</span><span class="s2">"</span>
<span class="k">raise</span> <span class="n">e</span>
</code></pre>
<p>As you can see, the <code>cause</code> of the error is not printed in any way to the console when error is raised:</p>
<pre><code>$ ruby /tmp/test.rb
Error: error with cause
Cause: GAHGAH
/tmp/test.rb:1:in `rescue in <main>': error with cause (RuntimeError)
from /tmp/test.rb:1:in `<main>
</code></pre> Ruby master - Feature #11323 (Open): Documentation update on how uniq works / guarantee of orderhttps://bugs.ruby-lang.org/issues/113232015-07-02T01:39:20Zwilburlo (Daniel Lo)wilburlo@gmail.com
<p>Greetings,</p>
<p>I was looking at Array.uniq and I was wondering how the code made the array unique.</p>
<p>There are 2 different possible outcomes for making an array unique.</p>
<p>For example:<br>
[1,2,1]</p>
<p>The first value is kept and all subsequent duplicate values are removed: [1,2]<br>
or<br>
The array is made unique, order is not retained: [2,1]</p>
<p>Would the ruby team consider adding a guarantee of order (first seen/first kept) is adding this to the Array.uniq specification? This is what happens in practice (irb), having this as part of the specification would be nice.</p>
<p>I looked at the code <a href="http://ruby-doc.org/core-2.2.1/Array.html#method-i-uniq" class="external">http://ruby-doc.org/core-2.2.1/Array.html#method-i-uniq</a> however, I wasn't able to determine exactly how it worked. :(</p>
<p>Thank you!</p>
<p>Regards,</p>
<p>Daniel Lo</p> Ruby master - Feature #11315 (Open): [PATCH] Add Array#^ for parity with other set-like operations.https://bugs.ruby-lang.org/issues/113152015-06-28T14:25:33Z0x0dea (D.E. Akers)0x0dea+redmine@gmail.com
<a name="Proposal"></a>
<h2 >Proposal<a href="#Proposal" class="wiki-anchor">¶</a></h2>
<p>It is proposed that <code>Array</code> be given an "XOR" method which returns the symmetric difference between itself and another array.</p>
<a name="Rationale"></a>
<h2 >Rationale<a href="#Rationale" class="wiki-anchor">¶</a></h2>
<p><code>#&</code> and <code>#|</code> need a friend to complete the set of set-like operations on <code>Array</code>.</p> Ruby master - Feature #11309 (Open): Iterator over string matcheshttps://bugs.ruby-lang.org/issues/113092015-06-26T14:55:43Zsawa (Tsuyoshi Sawada)
<p>This was hinted from a problem in stackoverflow (<a href="http://stackoverflow.com/questions/31074050/build-list-of-strings-containing-substrings-separated-by-an-from-a-string/31075511#31075511" class="external">http://stackoverflow.com/questions/31074050/build-list-of-strings-containing-substrings-separated-by-an-from-a-string/31075511#31075511</a>).</p>
<p>Suppose there is a string:</p>
<pre><code>s = "a_b_c_d_e"
</code></pre>
<p>To get an array of pre-matches that result from matching <code>s</code> with <code>"_"</code>, I can do this:</p>
<pre><code>a = []
s.scan("_"){a.push($`)}
a # => ["a", "a_b", "a_b_c", "a_b_c_d"]
</code></pre>
<p>But this looks too Perlish. I thought it would be nice if there is a method on <code>String</code> that creates an enumerator over matches so that I can do something like this:</p>
<pre><code>"a_b_c_d_e".some_method("_").with_object([]){|m, a| a.push(m.post_match)}
# => ["a", "a_b", "a_b_c", "a_b_c_d"]
</code></pre>
<p>where <code>m</code> is the last matchdata instance at that point. I believe such method would have wider application.</p> Ruby master - Feature #11305 (Open): [ipaddr] include the IP address in question within the Inval...https://bugs.ruby-lang.org/issues/113052015-06-25T04:25:17Zpostmodern (Hal Brodigan)postmodern.mod3@gmail.com
<p>The ipaddr.rb library has a father complex and custom parser. If an IP address fails to parse, it will raise an InvalidAddressError from one of it's many parsing methods. The IP address should be included in the exception message for context.</p> Ruby master - Misc #11295 (Open): Request for comments about error messageshttps://bugs.ruby-lang.org/issues/112952015-06-23T01:09:56Zko1 (Koichi Sasada)
<p>(This is not a proposal, bug reports)</p>
<p>Ruby shows error messages when something wrong.</p>
<p>There are several proposals to extend error messages.</p>
<ul>
<li><a href="https://github.com/charliesome/better_errors" class="external">https://github.com/charliesome/better_errors</a></li>
<li><a href="https://github.com/yuki24/did_you_mean" class="external">https://github.com/yuki24/did_you_mean</a></li>
<li><a href="https://github.com/ko1/pretty_backtrace" class="external">https://github.com/ko1/pretty_backtrace</a></li>
</ul>
<p>And some requests.</p>
<ul>
<li>Reverse backtrace and show error messages at the bottom (to avoid scroll up terminal)</li>
<li>Translation error messages to other languages</li>
</ul>
<p>If you have any idea, please tell us.<br>
We can consider about Ruby's error message APIs to realize your ideas.<br>
(I can't guarantee we can implement your ideas :p)</p> Ruby master - Feature #11148 (Open): Add a way to require files, but not raise an exception when ...https://bugs.ruby-lang.org/issues/111482015-05-12T22:48:44Ztenderlovemaking (Aaron Patterson)tenderlove@ruby-lang.org
<p>Hi,</p>
<p>I'm trying to make is so that RubyGems doesn't need to put directories on $LOAD_PATH (which is why I submitted Feature <a class="issue tracker-2 status-5 priority-4 priority-default closed" title="Feature: Allow rubygems' `require` to handle `autoload` calls (Closed)" href="https://bugs.ruby-lang.org/issues/11140">#11140</a>). I would like the <code>require</code> implemented in RubyGems to look up the file from a cache generated when the gem is installed, then pass a full file path to <code>require</code>.</p>
<p>The problem is that the user may have manipulated the load path somehow, and RubyGems needs to detect if the file is in the load path. Today, the algorithm inside RubyGems looks something like this:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">def</span> <span class="nf">require</span> <span class="n">file</span>
<span class="k">if</span> <span class="n">file_is_from_a_default_gem?</span><span class="p">(</span><span class="n">file</span><span class="p">)</span> <span class="c1"># this is so you can install new versions of default gems</span>
<span class="n">add_default_gem_to_loadpath</span>
<span class="k">end</span>
<span class="n">real_require</span> <span class="n">file</span>
<span class="k">rescue</span> <span class="no">LoadError</span>
<span class="n">gem</span> <span class="o">=</span> <span class="n">find_gem_that_contains_file</span><span class="p">(</span><span class="n">file</span><span class="p">)</span>
<span class="n">add_gem_to_loadpath</span> <span class="n">gem</span>
<span class="n">real_require</span> <span class="n">file</span>
<span class="k">end</span>
</code></pre>
<p>Instead of adding the directory to the load path, I would like to look up the full file path from a cache that is generated when the gem is installed. If we had a cache, that means the new implementation would look like this:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">def</span> <span class="nf">require</span> <span class="n">file</span>
<span class="k">if</span> <span class="n">file_is_from_a_default_gem?</span><span class="p">(</span><span class="n">file</span><span class="p">)</span> <span class="c1"># this is so you can install new versions of default gems</span>
<span class="n">add_default_gem_to_loadpath</span>
<span class="k">end</span>
<span class="n">real_require</span> <span class="n">file</span> <span class="c1"># get slower as paths are added to LOAD_PATH</span>
<span class="k">rescue</span> <span class="no">LoadError</span>
<span class="n">gem</span> <span class="o">=</span> <span class="n">find_gem_that_contains_file</span><span class="p">(</span><span class="n">file</span><span class="p">)</span> <span class="c1"># use a cache so lookup is O(1)</span>
<span class="n">fully_qualified_path</span> <span class="o">=</span> <span class="n">gem</span><span class="p">.</span><span class="nf">full_path</span> <span class="n">file</span>
<span class="n">real_require</span> <span class="n">fully_qualified_path</span> <span class="c1"># send a fully qualified path, so LOAD_PATH isn't searched</span>
<span class="k">end</span>
</code></pre>
<p>Unfortunately, that means that every call to require in the system would raise an exception. I'd like to add a version of <code>require</code> that we can call that <em>doesn't</em> raise an exception. Then I could write the code like this:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">def</span> <span class="nf">require</span> <span class="n">file</span>
<span class="k">if</span> <span class="n">file_is_from_a_default_gem?</span><span class="p">(</span><span class="n">file</span><span class="p">)</span> <span class="c1"># this is so you can install new versions of default gems</span>
<span class="n">add_default_gem_to_loadpath</span>
<span class="k">end</span>
<span class="n">found</span> <span class="o">=</span> <span class="n">try_require</span> <span class="n">file</span>
<span class="k">if</span> <span class="kp">nil</span> <span class="o">==</span> <span class="n">found</span>
<span class="n">gem</span> <span class="o">=</span> <span class="n">find_gem_that_contains_file</span><span class="p">(</span><span class="n">file</span><span class="p">)</span> <span class="c1"># use a cache so lookup is O(1)</span>
<span class="n">fully_qualified_path</span> <span class="o">=</span> <span class="n">gem</span><span class="p">.</span><span class="nf">full_path</span> <span class="n">file</span>
<span class="n">real_require</span> <span class="n">fully_qualified_path</span> <span class="c1"># send a fully qualified path, so LOAD_PATH isn't searched</span>
<span class="k">end</span>
<span class="n">found</span>
<span class="k">end</span>
<span class="k">end</span>
</code></pre>
<p>This would keep the load path small, and prevent exceptions from happening during the "normal" case.</p>
<p>I've attached a patch that implements <code>try_require</code>, but I'm not set on the name. Maybe doing <code>require(file, exception: false)</code> would work too.</p> Ruby master - Feature #10949 (Open): Time is WB unprotectedhttps://bugs.ruby-lang.org/issues/109492015-03-08T18:10:04Zsylvain.joyeux (Sylvain Joyeux)
<p>Ruby's built-in Time object is shady. Unfortunately, it is an object that can commonly be created in big numbers (think logs or stuff like that), and since they don't store relationship with other objects they are perfect candidate for the oldgen.</p>
<p>I don't really know if there is a kind of policy about which built-in Ruby objects should be sunny or not, so I just put this as a feature request (as opposed to having it as a bug)</p> Ruby master - Feature #10927 (Open): [PATCH] Add default empty string to string replacementshttps://bugs.ruby-lang.org/issues/109272015-03-02T16:26:41Zzimbatm (zimba tm)zimbatm@zimbatm.com
<p>Hi ruby devs !</p>
<p>A common case for string substitution is to just remove the found items. This patch changes the <code>String#[g]sub[!]</code> methods to allow that.</p>
<p>Old usages:</p>
<pre><code>"foo".sub("o", "") #=> "fo"
"foo".gsub("o", "") #=> "f"
</code></pre>
<p>New usages:</p>
<pre><code>"foo".sub("o") #=> "fo"
"foo".gsub("o") #=> "f"
</code></pre>
<p>Applies to the bang versions as well.</p>
<p>This commit changes the return values of String#gsub! call with a single<br>
argument and no block from an Enumerator to a String. The previous usage<br>
would return an Enumerator containing each match, which is not a string<br>
substitution. A suitable replacement should nonetheless be found before<br>
merging this commit.</p>
<p>Before:</p>
<pre><code>"foo".gsub("o") #=> #<Enumerator: "foo":gsub("o")>
</code></pre>
<p>Replacement:</p>
<pre><code>???
</code></pre> Ruby master - Feature #10882 (Open): Provide Levenshtein distance implementation as part of stdlibhttps://bugs.ruby-lang.org/issues/108822015-02-21T20:50:04Zyuki24 (Yuki Nishijima)yk.nishijima@gmail.com
<p><a href="http://en.wikipedia.org/wiki/Levenshtein_distance" class="external">Levenshtein distance algorithm</a> has been used by Rubygems, Bundler, did_you_mean and Rails and I think it's popular enough to provide it as part of Ruby's stdlib. It still seems a bit too high-level though, but definitely useful (e.g. <a href="https://github.com/ruby/rake/pull/29" class="external">adding "did you mean?" to rake</a>).</p>
<p>API-wise, I would like to propose something like the following, but I'm totally open to hear the core team's opinions as I'm not sue if this is great.</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="nb">require</span> <span class="s1">'distance'</span>
<span class="no">Distance</span><span class="p">.</span><span class="nf">levenshtein</span><span class="p">(</span><span class="n">str1</span><span class="p">,</span> <span class="n">str2</span><span class="p">)</span>
</code></pre>
<p>It would also be interesting to have <code>#distance</code> method on <code>String</code>:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="s2">"word"</span><span class="p">.</span><span class="nf">distance</span><span class="p">(</span><span class="s2">"other"</span><span class="p">)</span>
</code></pre>
<p>which is implemented as:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">def</span> <span class="nf">distance</span><span class="p">(</span><span class="n">str</span><span class="p">,</span> <span class="n">algorithm</span> <span class="o">=</span> <span class="ss">:levenshtein</span><span class="p">)</span>
<span class="c1"># calculate the distance here.</span>
<span class="k">end</span>
</code></pre>
<p>so it can allow to change the algorythm when we add more (e.g. <a href="http://en.wikipedia.org/wiki/Jaro%E2%80%93Winkler_distance" class="external">Jaro–Winkler distance</a>).</p> Ruby master - Feature #10879 (Open): UnboundMethod#to_prochttps://bugs.ruby-lang.org/issues/108792015-02-21T19:56:44ZGondolin (Damien Robert)Damien.Olivier.Robert+ruby@gmail.com
<p>Since unbound methods from modules can now be bound to any object, I use them a lot.<br>
I think it would be very natural to be able to convert them to Proc:</p>
<pre><code>module UnboundMethod
def to_proc
return lambda do |obj,*args,&b|
self.bind(obj).call(*args,&b)
end
end
end
</code></pre>
<p>This would allow things like</p>
<pre><code>module Foo
def foo
self+"foo"
end
end
["bar","baz"].map(&Foo.instance_method(:foo)) => ["barfoo", "bazfoo"]
</code></pre> Ruby master - Feature #10869 (Open): Add support for option to pre-compile Ruby fileshttps://bugs.ruby-lang.org/issues/108692015-02-19T15:54:01Zrosenfeld (Rodrigo Rosenfeld Rosas)rr.rosas@gmail.com
<p>I know this topic is tricky but please bear with me.</p>
<p>Goal: improve performance on files loading to speed up the boot process of some application requiring lots of files/gems.</p>
<p>Background:</p>
<p>Currently most frameworks/gems rely on the autoload feature to allow applications to load faster by lazy loading files as missing constants are referenced.</p>
<p>Autoload behavior may lead to hard-to-understand bugs and I believe this is the main reason why Matz discourages its usage:</p>
<p><a href="https://bugs.ruby-lang.org/issues/5653" class="external">https://bugs.ruby-lang.org/issues/5653</a></p>
<p>I described a bug involving autoload in a real scenario in this comment of this same issue:</p>
<p><a href="https://bugs.ruby-lang.org/issues/5653#note-26" class="external">https://bugs.ruby-lang.org/issues/5653#note-26</a></p>
<p>While I agree that autoload should be discouraged I think we should provide an alternative for speeding up application loading.</p>
<p>Overall benchmarks:</p>
<p>I decided to create a simple benchmark in order to measure how much time MRI would take to load 10_000 files containing a hundred methods each:</p>
<pre><code>10000.times{|j| File.open("test#{j}.rb", 'w'){|f|f.puts "class A#{j}"; 100.times{|i| f.puts " def m#{i}; end"}; f.puts "end"}}
time ruby -r benchmark -I. -e 'puts Benchmark.realtime{10000.times{|i|require "test#{i}"}}
8.766814350005006
real 0m10.068s
user 0m9.416s
sys 0m0.532s
time cat test*.rb > /dev/null
real 0m0.107s
user 0m0.068s
sys 0m0.040s
</code></pre>
<p>As you can see, most of the time is spent on MRI itself rather than on disk. Using require_relative doesn't make any real difference either.</p>
<p>Suggested solution: Pre-compiled files</p>
<p>I know nothing about MRI internals but I suspect that maybe if MRI could support some sort of database containing a precompiled version of the files (the bytecodes maybe). The database would store the size and a hash for each processed file. If the size and hash remain the same it would assume the bytecodes in the database are up-to-date, which should happen in most cases. In this case those files could be possibly loaded much faster.</p>
<p>In order to avoid additional overhead or some bugs in some cases, maybe an option to enable the pre-compile behavior would be better to allow us to test this approach.</p>
<p>I understand that it may be complicated to precompile all kind of Ruby files as they could execute code as well rather than simply declaring classes. In such cases I still think it would worth to detect such cases and skip pre-compiling for such files and only pre-compile those files containing simple class declarations only, which is the case for a lot of files already. Maybe this could potentially make gem owners move their statements to a separate file in order to allow the classes to be precompiled in the future...</p>
<p>Do you have any other suggestions to speed up application loading that do not involve autoload and conditional requires? Do you think precompilation is possible/worthy on MRI?</p> Ruby master - Feature #10863 (Open): allow protected class methods to be callable from instance m...https://bugs.ruby-lang.org/issues/108632015-02-17T22:36:20Zbughit (bug hit)
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">class</span> <span class="nc">Bar</span>
<span class="k">class</span> <span class="o"><<</span> <span class="nb">self</span>
<span class="kp">protected</span> <span class="k">def</span> <span class="nf">foo_helper</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="k">def</span> <span class="nf">foo</span>
<span class="nb">self</span><span class="p">.</span><span class="nf">class</span><span class="p">.</span><span class="nf">foo_helper</span>
<span class="k">end</span>
<span class="k">end</span>
</code></pre>
<p>here, Bar::foo_helper is not intended as the api of the class, it's a helper for internal use so it should be possible to use it internally without making it public or resorting to <code>send</code></p> Ruby master - Feature #10851 (Open): Introduce Regexp#fetchhttps://bugs.ruby-lang.org/issues/108512015-02-13T11:15:17Zprijutme4ty (Ilya Vorontsov)prijutme4ty@gmail.com
<p>There is a common pattern in code:<br>
match = pattern.match(string)<br>
var = match && match[name_of_capture]</p>
<p>One should write it everywhere not to get an exception. It can be solved by introducing something like Hash#fetch.</p>
<p>class Regexp<br>
def fetch(string, capture_name:, pos: nil, default_value: nil)<br>
m = match(string, pos)<br>
m ? m[capture_name] : default_value<br>
end<br>
end</p> Ruby master - Feature #10770 (Open): chr and ord behavior for ill-formed byte sequences and surro...https://bugs.ruby-lang.org/issues/107702015-01-22T01:09:37Zmasakielastic (Masaki Kagaya)masakielastic@gmail.com
<p><code>ord</code> raises error when meeting ill-formed byte sequences, thus the difference of atttiute exists between <code>each_char</code> and <code>each_codepoint</code>.</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">str</span> <span class="o">=</span> <span class="s2">"a</span><span class="se">\x80</span><span class="s2">bc"</span>
<span class="n">str</span><span class="p">.</span><span class="nf">each_char</span> <span class="p">{</span><span class="o">|</span><span class="n">c</span><span class="o">|</span> <span class="nb">puts</span> <span class="n">c</span> <span class="p">}</span>
<span class="c1"># no error</span>
<span class="n">str</span><span class="p">.</span><span class="nf">each_codepoint</span> <span class="p">{</span><span class="o">|</span><span class="n">c</span><span class="o">|</span> <span class="nb">puts</span> <span class="n">c</span> <span class="p">}</span>
<span class="c1"># invalid byte sequence in UTF-8 (ArgumentError)</span>
</code></pre>
<p>The one way of keeping consistency is change <code>ord</code> to return substitute code point such as 0xFFFD adopted by <code>scrub</code>.</p>
<p>Another problem about consitency is surrogate code points. Althouh CRuby allows to use surrogate code points in unicode literal, <code>ord</code> and <code>chr</code> don't allow them.</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="s2">"</span><span class="se">\u</span><span class="s2">D800"</span><span class="p">.</span><span class="nf">ord</span>
<span class="c1"># invalid byte sequence in UTF-8 (ArgumentError)</span>
<span class="mh">0xD800</span><span class="p">.</span><span class="nf">chr</span><span class="p">(</span><span class="s1">'UTF-8'</span><span class="p">)</span>
<span class="c1"># invalid codepoint 0xD800 in UTF-8 (RangeError)</span>
</code></pre>
<p>How about remove the restriction? The one example of using surrogate code points is converting a 4-byte character to a pair of 3-byte characters for MySQL/MariaDB's utf8mb3.</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">str</span> <span class="o">=</span> <span class="s2">"</span><span class="se">\u</span><span class="s2">{1F436}"</span> <span class="c1"># DOG FACE</span>
<span class="n">cp</span> <span class="o">=</span> <span class="n">str</span><span class="p">.</span><span class="nf">ord</span>
<span class="k">if</span> <span class="n">cp</span> <span class="o">></span> <span class="mh">0x10000</span> <span class="k">then</span>
<span class="c1"># http://unicode.org/faq/utf_bom.html#utf16-4</span>
<span class="n">lead</span> <span class="o">=</span> <span class="mh">0xD800</span> <span class="o">-</span> <span class="p">(</span><span class="mh">0x10000</span> <span class="o">>></span> <span class="mi">10</span><span class="p">)</span> <span class="o">+</span> <span class="p">(</span><span class="n">cp</span> <span class="o">>></span> <span class="mi">10</span><span class="p">)</span>
<span class="n">trail</span> <span class="o">=</span> <span class="mh">0xDC00</span> <span class="o">+</span> <span class="p">(</span><span class="n">cp</span> <span class="o">&</span> <span class="mh">0x3FF</span><span class="p">)</span>
<span class="n">ret</span> <span class="o">=</span> <span class="n">lead</span><span class="p">.</span><span class="nf">chr</span><span class="p">(</span><span class="s1">'UTF-8'</span><span class="p">)</span> <span class="o">+</span> <span class="n">trail</span><span class="p">.</span><span class="nf">chr</span><span class="p">(</span><span class="s1">'UTF-8'</span><span class="p">)</span>
<span class="k">end</span>
</code></pre> Ruby master - Feature #10729 (Open): Array method to subtract in placehttps://bugs.ruby-lang.org/issues/107292015-01-10T22:36:02Zsawa (Tsuyoshi Sawada)
<p>I request a method on array that takes another array, subtract that from self in place (= destructively), and return the subtracted elements:</p>
<pre><code>a = [1, 2, 3, 4, 5]
a.some_method([2, 4, 6]) #=> [2, 4]
a #=> [1, 3, 5]
</code></pre>
<p>Ideally, it should also allow a block:</p>
<pre><code>a = [1, 2, 3, 4, 5]
a.some_method(&:even?) #=> [2, 4]
a #=> [1, 3, 5]
</code></pre>
<p>This operation is quite frequent, but at the moment, it requires several steps to do.</p> Ruby master - Feature #10728 (Open): Warning for Fixnum#size to use RbConfig::SIZEOF['long']https://bugs.ruby-lang.org/issues/107282015-01-10T11:42:23Zakr (Akira Tanaka)akr@fsij.org
<p>How about add a warning for Fixnum#size ?</p>
<pre><code>% ./ruby -e 'p 0.size'
-e:1: warning: Use RbConfig::SIZEOF['long'] instead of Fixnum#size
8
</code></pre>
<p>Currently it returns sizeof(long) which is 4 or 8.</p>
<p>However it is implementation detail and<br>
it may difficult to define it in Ruby implementations other than CRuby.</p>
<p>There is a way to obtain sizeof(long): RbConfig::SIZEOF['long']<br>
It works since Ruby 2.1.</p>
<pre><code>% ruby -rrbconfig/sizeof -e "p RbConfig::SIZEOF['long']"
8
</code></pre>
<p>So the warning is a migration path to use RbConfig::SIZEOF['long'].</p>
<p>Note that this may affect many code.<br>
So this issue may irritate many people.<br>
fixnum-size-search.txt is a search result in gems.</p> Ruby master - Feature #10726 (Open): [PATCH 4/4] * New methods: Set#powerhttps://bugs.ruby-lang.org/issues/107262015-01-10T01:22:31Zgogotanaka (Kazuki Tanaka)mail@tanakakazuki.com
<p>Hi, there.</p>
<p>Here are 4 patches.</p>
<pre><code>implement_set#power.patch ... Implement Set#power
update_test_set.rb.patch ... Add tests for Set#power
update_NEWS.patch ... Update NEWS for Set#power
fix_indent_NEWS.patch ... Adjustment indent of NEWS
</code></pre>
<p>take your time.</p>
<p>gogo.</p> Ruby master - Feature #10701 (Open): Class:Array 2 New methodshttps://bugs.ruby-lang.org/issues/107012015-01-06T14:48:37Zkruiserx (Eugene Kuhn)kruiserx@gmail.com
<p>Hi,</p>
<p>New to this, but this is an Array method that I use a lot and thought it might be included in a release, it's basic, but very helpful when you need to rotate certain defined values ie. log rotation with monthly timestamps</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">class</span> <span class="nc">Array</span>
<span class="k">def</span> <span class="nf">next</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
<span class="c1">#Returns next element value (and loop to the beginning if last element is matched ) based on first input found. if not found, returns first value of array</span>
<span class="nb">self</span><span class="p">[((</span><span class="nb">self</span><span class="p">.</span><span class="nf">index</span><span class="p">(</span><span class="n">value</span><span class="p">)</span><span class="o">||-</span><span class="mi">1</span><span class="p">)</span><span class="o">+</span><span class="mi">1</span><span class="p">)</span><span class="o">%</span><span class="nb">self</span><span class="p">.</span><span class="nf">size</span><span class="p">]</span>
<span class="k">end</span>
<span class="k">def</span> <span class="nf">prev</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
<span class="c1">#Returns previous element value (and loop to the end if first element is matched ) based on first input found. if not found, returns first value of array</span>
<span class="nb">self</span><span class="p">[((</span><span class="nb">self</span><span class="p">.</span><span class="nf">index</span><span class="p">(</span><span class="n">value</span><span class="p">)</span><span class="o">||</span><span class="mi">1</span><span class="p">)</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span><span class="o">%</span><span class="nb">self</span><span class="p">.</span><span class="nf">size</span><span class="p">]</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="n">arr1</span> <span class="o">=</span> <span class="p">[</span><span class="s1">'Jan'</span><span class="p">,</span><span class="s1">'Feb'</span><span class="p">,</span><span class="s1">'Mar'</span><span class="p">,</span><span class="s1">'Apr'</span><span class="p">,</span><span class="s1">'May'</span><span class="p">,</span><span class="s1">'Jun'</span><span class="p">,</span><span class="s1">'Jul'</span><span class="p">,</span><span class="s1">'Aug'</span><span class="p">,</span><span class="s1">'Sep'</span><span class="p">,</span><span class="s1">'Oct'</span><span class="p">,</span><span class="s1">'Nov'</span><span class="p">,</span><span class="s1">'Dec'</span><span class="p">]</span>
<span class="nb">puts</span> <span class="n">arr1</span><span class="p">.</span><span class="nf">next</span><span class="p">(</span><span class="s2">""</span><span class="p">)</span> <span class="c1">#=> returns "Jan"</span>
<span class="nb">puts</span> <span class="n">arr1</span><span class="p">.</span><span class="nf">prev</span><span class="p">(</span><span class="s2">""</span><span class="p">)</span> <span class="c1">#=> returns "Jan"</span>
<span class="nb">puts</span> <span class="n">arr1</span><span class="p">.</span><span class="nf">next</span><span class="p">(</span><span class="s2">"Apr"</span><span class="p">)</span> <span class="c1">#=> returns "May"</span>
<span class="nb">puts</span> <span class="n">arr1</span><span class="p">.</span><span class="nf">next</span><span class="p">(</span><span class="s2">"Dec"</span><span class="p">)</span> <span class="c1">#=> returns "Jan"</span>
<span class="nb">puts</span> <span class="n">arr1</span><span class="p">.</span><span class="nf">prev</span><span class="p">(</span><span class="s2">"Jan"</span><span class="p">)</span> <span class="c1">#=> returns "Dec"</span>
<span class="nb">puts</span> <span class="n">arr1</span><span class="p">.</span><span class="nf">prev</span><span class="p">(</span><span class="s2">"Apr"</span><span class="p">)</span> <span class="c1">#=> returns "Mar"</span>
</code></pre> Ruby master - Feature #10683 (Open): fix inconsistent behavior of Kernel.Hash()https://bugs.ruby-lang.org/issues/106832014-12-31T19:35:39Zrecursive-madman (Recursive Madman)recursive.madman@gmx.de
<p>I find the way the global function <code>Hash</code> (aka <code>Kernel.Hash</code>) works a bit confusing.</p>
<p>To illustrate:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="no">Hash</span><span class="p">(</span><span class="kp">nil</span><span class="p">)</span> <span class="c1">#=> {} (1)</span>
<span class="no">Hash</span><span class="p">({})</span> <span class="c1">#=> {} (2)</span>
<span class="no">Hash</span><span class="p">([])</span> <span class="c1">#=> {} (3)</span>
<span class="c1"># but</span>
<span class="no">Hash</span><span class="p">([[</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">]])</span> <span class="c1">#! TypeError (4)</span>
</code></pre>
<p>Case (1) and (2) make perfect sense to me (calling <code>Hash(var)</code> when <code>var</code> is an optional argument defaulting to <code>nil</code> will always give a (possibly empty) Hash or a TypeError, which is very useful).<br>
Case (3) however seems inconsistent, since (4) doesn't work.</p>
<p>To contrast this with the respective <code>String</code> function:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="no">String</span><span class="p">([])</span> <span class="c1">#=> "[]"</span>
<span class="no">String</span><span class="p">(</span><span class="s1">''</span><span class="p">)</span> <span class="c1">#=> ""</span>
<span class="no">String</span><span class="p">({})</span> <span class="c1">#=> "{}"</span>
<span class="no">String</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span> <span class="c1">#=> "0"</span>
<span class="no">String</span><span class="p">(</span><span class="kp">nil</span><span class="p">)</span> <span class="c1">#=> ""</span>
</code></pre>
<p>it seems that calling <code>String(obj)</code> is equivalent to calling <code>obj.to_s</code>.</p>
<p>Thus I would assume <code>Hash(obj)</code> being equivalent to calling <code>obj.to_h</code>.</p>
<p>It is not though (calling <code>to_h</code> on <code>[[1,2]]</code> gives <code>{1=>2}</code>, while using <code>Hash()</code> raises a <code>TypeError</code>).</p>
<p>I propose to do <strong>one</strong> of the following changes:</p>
<ul>
<li>either remove the special handling of <code>[]</code>, such that only <code>nil</code> or a <code>Hash</code> are valid values to be passed to <code>Hash()</code>, or</li>
<li>change <code>Hash()</code> to call <code>to_h</code> on it's argument, when the argument is neither <code>nil</code> nor a <code>Hash</code>.</li>
</ul> Ruby master - Feature #10663 (Open): Consider adding support for String input to File. methodshttps://bugs.ruby-lang.org/issues/106632014-12-27T17:07:22Zshevegen (Robert A. Heiler)shevegen@gmail.com
<p>Hi,</p>
<p>Consider a file called 'test.rb'</p>
<p>We can change its permission with File.chmod like so:</p>
<p>File.chmod 444,'test.rb'</p>
<p>This should make it readable (world, group, owner)</p>
<p>and I believe it would be synonymous to</p>
<p>chmod +r test.rb</p>
<p>on the commandline.</p>
<p>In Ruby, this here does not work:</p>
<p>File.chmod '+r','test.rb'<br>
TypeError: no implicit conversion of String into Integer</p>
<p>Is there a reason why it must be an Integer? If Strings would<br>
be allowed then we could also use those abbreviations like<br>
'+r' just as we can with the commandline utility chmod.</p> Ruby master - Feature #10645 (Open): Consider adding support of .first to MatchData object like M...https://bugs.ruby-lang.org/issues/106452014-12-25T04:13:13Zshevegen (Robert A. Heiler)shevegen@gmail.com
<p>Hi,</p>
<p>Lately I was using match data</p>
<p>test_string = "</p><a name="This-is-heading-6"></a>
<h6 >This is heading 6<a href="#This-is-heading-6" class="wiki-anchor">¶</a></h6>"
<p>match = /<h\d+>(.+)</h\d+>/.match(test_string)</p>
<p>Then I tried to do this:</p>
<p>puts match.first</p>
<p>It did not work.</p>
<p>puts match[0]</p>
<p>Is the way to go.</p>
<p>My question is:</p>
<p>Would it be consistent to consider adding<br>
support of .first to MatchData as well?</p>
<p>class Array has it - I kind of assumed that<br>
MatchData is a bit similar to Array.</p>
<p>Thanks,</p> Ruby master - Feature #10589 (Open): [TracePoint API] Make THREAD_{BEGIN, END} events return some...https://bugs.ruby-lang.org/issues/105892014-12-11T16:27:00Zdeivid (David Rodríguez)
<p>Currently the :thread_begin and thread_end events return no context information at all</p>
<pre><code>TracePoint.trace(:thread_begin, :thread_end) do |tp|
puts "#{tp.defined_class}::#{tp.method_id}@#{tp.path}:#{tp.lineno}"
end
t = Thread.new do
sleep 1
end
t.join
</code></pre>
<p>prints just</p>
<pre><code>"Hi thread, ::@:0"
"Bye thread, ::@:0"
</code></pre>
<p>It'd be nice if they gave at least some file:line context information about the thread.</p>
<p>What do you think, <a class="user active user-mention" href="https://bugs.ruby-lang.org/users/17">@ko1 (Koichi Sasada)</a>? Would this be possible?</p>
<p>Thanks a lot</p> Ruby master - Feature #10585 (Open): struct: speedup struct.attr = v for first 10 attributes and ...https://bugs.ruby-lang.org/issues/105852014-12-10T14:41:30Zfunny_falcon (Yura Sokolov)funny.falcon@gmail.com
<p>0001 - Define optimized setters for first 10 attributes.</p>
<p>0002 - Cache members definition in an subclasses - it is safe cause it could be modified/overloaded.<br>
And use <code>rb_attr_get</code> to lookup definition - it is safe cause result is checked later and <code>Qnil</code> is treated as error.</p>
<p>0003,0004 - Use custom hash structure (on top of Array) to lookup members index in a big structure.<br>
Well, I doubt that big structures are useful, so I will not grieve if 0004 is not accepted.</p> Ruby master - Feature #10552 (Open): [PATCH] Add Enumerable#frequencies and Enumerable#relative_f...https://bugs.ruby-lang.org/issues/105522014-11-27T07:59:05Zbrianhempel (Brian Hempel)plasticchicken@gmail.com
<p>Counting how many times a value appears in some collection has always been a bit clumsy in Ruby. While Ruby has enough constructs to do it in one line, it still requires knowing the folklore of the optimum solution as well as some acrobatic typing:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="sx">%w[cat bird bird horse]</span><span class="p">.</span><span class="nf">each_with_object</span><span class="p">(</span><span class="no">Hash</span><span class="p">.</span><span class="nf">new</span><span class="p">(</span><span class="mi">0</span><span class="p">))</span> <span class="p">{</span> <span class="o">|</span><span class="n">word</span><span class="p">,</span> <span class="nb">hash</span><span class="o">|</span> <span class="nb">hash</span><span class="p">[</span><span class="n">word</span><span class="p">]</span> <span class="o">+=</span> <span class="mi">1</span> <span class="p">}</span>
<span class="c1"># => {"cat" => 1, "bird" => 2, "horse" => 1}</span>
</code></pre>
<p>What if Ruby could count for us? This patch adds two methods to enumerables:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="sx">%w[cat bird bird horse]</span><span class="p">.</span><span class="nf">frequencies</span>
<span class="c1"># => {"bird" => 2, "horse" => 1, "cat" => 1}</span>
<span class="sx">%w[cat bird bird horse]</span><span class="p">.</span><span class="nf">relative_frequencies</span>
<span class="c1"># => {"bird" => 0.5, "horse" => 0.25, "cat" => 0.25}</span>
</code></pre>
<p>To make programmers happier, the returned hash has the most common values first. This is nice because, for example, finding the most common element of a collection becomes trivial:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">most_common</span><span class="p">,</span> <span class="n">count</span> <span class="o">=</span> <span class="sx">%w[cat bird bird horse]</span><span class="p">.</span><span class="nf">frequencies</span><span class="p">.</span><span class="nf">first</span>
</code></pre>
<p>Whereas the best you can do with vanilla Ruby is:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">most_common</span><span class="p">,</span> <span class="n">count</span> <span class="o">=</span> <span class="sx">%w[cat bird bird horse]</span><span class="p">.</span><span class="nf">each_with_object</span><span class="p">(</span><span class="no">Hash</span><span class="p">.</span><span class="nf">new</span><span class="p">(</span><span class="mi">0</span><span class="p">))</span> <span class="p">{</span> <span class="o">|</span><span class="n">word</span><span class="p">,</span> <span class="nb">hash</span><span class="o">|</span> <span class="nb">hash</span><span class="p">[</span><span class="n">word</span><span class="p">]</span> <span class="o">+=</span> <span class="mi">1</span> <span class="p">}.</span><span class="nf">max_by</span><span class="p">(</span><span class="o">&</span><span class="ss">:last</span><span class="p">)</span>
<span class="c1"># or...</span>
<span class="n">most_common</span><span class="p">,</span> <span class="n">count</span> <span class="o">=</span> <span class="sx">%w[cat bird bird horse]</span><span class="p">.</span><span class="nf">group_by</span><span class="p">(</span><span class="o">&</span><span class="ss">:to_s</span><span class="p">).</span><span class="nf">map</span> <span class="p">{</span> <span class="o">|</span><span class="n">word</span><span class="p">,</span> <span class="n">arr</span><span class="o">|</span> <span class="p">[</span><span class="n">word</span><span class="p">,</span> <span class="n">arr</span><span class="p">.</span><span class="nf">size</span><span class="p">]</span> <span class="p">}.</span><span class="nf">max_by</span><span class="p">(</span><span class="o">&</span><span class="ss">:last</span><span class="p">)</span>
</code></pre>
<p>While I don't like the long method names, "frequencies" and "relative frequencies" are the terms used in basic statistics. <a href="http://en.wikipedia.org/wiki/Frequency_%28statistics%29" class="external">http://en.wikipedia.org/wiki/Frequency_%28statistics%29</a></p> Ruby master - Feature #10519 (Open): TLS Renegotiationhttps://bugs.ruby-lang.org/issues/105192014-11-16T12:24:37Zbararchy (Bar Hofesh)bar.hofesh@safe-t.com
<p>Hello,</p>
<p>It seems that the ability to force renegotiation as a client is missing or not exposed.<br>
The ability is implemented in OpenSSL's s_client as the "R" character when the connection is established.<br>
It will be great if that can be exposed to ruby's OpenSSL::SSL::SSLSocket</p>
<p>Thanks,</p> Ruby master - Feature #10505 (Open): [PATCH 2/n] 同相条件を扱いたい./Object#eql? with block. (ja/en)https://bugs.ruby-lang.org/issues/105052014-11-13T00:31:08Zgogotanaka (Kazuki Tanaka)mail@tanakakazuki.com
<p>English follows japanese</p>
<a name="文脈"></a>
<h2 >文脈<a href="#文脈" class="wiki-anchor">¶</a></h2>
<p>両辺に同じ処理を施してその同値性を確かめたいという場面は往々にしてあると思います.</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">a</span> <span class="o">%</span> <span class="mi">2</span> <span class="o">==</span> <span class="n">b</span> <span class="o">%</span> <span class="mi">2</span>
<span class="n">a</span><span class="p">.</span><span class="nf">abs</span> <span class="o">==</span> <span class="n">b</span><span class="p">.</span><span class="nf">abs</span>
<span class="s2">"RUBY"</span><span class="p">.</span><span class="nf">downcase</span> <span class="o">==</span> <span class="s2">"Ruby"</span><span class="p">.</span><span class="nf">downcase</span>
</code></pre>
<p>この際、施す処理を両辺に書くのは嫌だというのが今回のチケットの主たるモチベーションになります.</p>
<p>特に長い処理ともなると尚更だと思います.<br>
(そもそも長い処理を両辺に施すコンテキストがイカン!という批判は甘んじて受け入れますが)</p>
<a name="提案"></a>
<h2 >提案<a href="#提案" class="wiki-anchor">¶</a></h2>
<p>施す処理を各オブジェクトの<code>#eql?</code>に対してブロックとして渡せるようにしたいです.</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="c1"># 2を法とする合同式</span>
<span class="n">a</span><span class="p">.</span><span class="nf">eql?</span><span class="p">(</span><span class="n">b</span><span class="p">)</span> <span class="p">{</span> <span class="o">|</span><span class="n">n</span><span class="o">|</span> <span class="n">n</span> <span class="o">%</span> <span class="mi">2</span> <span class="p">}</span>
<span class="c1"># 意味論的にあまり好きでないですが、ブロックをシンボルで渡す事も可能とします.</span>
<span class="s2">"RUBY"</span><span class="p">.</span><span class="nf">eql?</span><span class="p">(</span><span class="s2">"Ruby"</span><span class="p">,</span> <span class="o">&</span><span class="ss">:downcase</span><span class="p">)</span>
</code></pre>
<a name="懸念点"></a>
<h2 >懸念点<a href="#懸念点" class="wiki-anchor">¶</a></h2>
<ul>
<li>block処理後は<code>#===</code>で比較するので挙動に若干の違和感.</li>
</ul>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="mi">1</span><span class="p">.</span><span class="nf">eql?</span><span class="p">(</span><span class="mf">1.0</span><span class="p">,</span> <span class="o">&</span><span class="ss">:itself</span><span class="p">)</span>
<span class="c1"># => true</span>
<span class="mi">1</span><span class="p">.</span><span class="nf">eql?</span><span class="p">(</span><span class="mf">1.0</span><span class="p">)</span>
<span class="c1"># => false</span>
</code></pre>
<ul>
<li>既存のコードで<code>#eql?</code>にブロックを渡している可能性がある?</li>
</ul>
<p>変更の範囲も広くなりそうなのと見積もれてない懸念点がある気がしたので、ひとまず<code>Numeric#eql?</code>だけ実装してみました<br>
しばらく様子を見て問題がなければ他のオブジェクト群に対しても実装したいかとも思います.</p>
<p>よろしくお願い致します.</p>
<a name="Motivation"></a>
<h2 >Motivation<a href="#Motivation" class="wiki-anchor">¶</a></h2>
<p>We often encounter a situation where we need to compare both operands after certain function apply for these.</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">a</span> <span class="o">%</span> <span class="mi">2</span> <span class="o">==</span> <span class="n">b</span> <span class="o">%</span> <span class="mi">2</span>
<span class="n">a</span><span class="p">.</span><span class="nf">abs</span> <span class="o">==</span> <span class="n">b</span><span class="p">.</span><span class="nf">abs</span>
<span class="s2">"RUBY"</span><span class="p">.</span><span class="nf">downcase</span> <span class="o">==</span> <span class="s2">"Ruby"</span><span class="p">.</span><span class="nf">downcase</span>
</code></pre>
<p>I am not willing to write same function both operands.<br>
This is main motivation.</p>
<a name="Proposal"></a>
<h2 >Proposal<a href="#Proposal" class="wiki-anchor">¶</a></h2>
<p>Now if the optional block is given, compare between results of running block for both operands.</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="c1"># check wether `a` and `b` are congruent modulo 2 or not</span>
<span class="n">a</span><span class="p">.</span><span class="nf">eql?</span><span class="p">(</span><span class="n">b</span><span class="p">)</span> <span class="p">{</span> <span class="o">|</span><span class="n">n</span><span class="o">|</span> <span class="n">n</span> <span class="o">%</span> <span class="mi">2</span> <span class="p">}</span>
<span class="s2">"RUBY"</span><span class="p">.</span><span class="nf">eql?</span><span class="p">(</span><span class="s2">"Ruby"</span><span class="p">,</span> <span class="o">&</span><span class="ss">:downcase</span><span class="p">)</span>
</code></pre>
<p>I've found similar issue <a href="https://bugs.ruby-lang.org/issues/10426" class="external">here</a></p>
<p>For now, I've implemented for <code>Numeric#eql?</code><br>
If it looks like good for you, I'm gonna implement for other Classes</p>
<a name="Concern"></a>
<h2 >Concern<a href="#Concern" class="wiki-anchor">¶</a></h2>
<ul>
<li>We'll compare by using <code>#===</code> after passed to block. so it may look like weird.</li>
</ul>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="mi">1</span><span class="p">.</span><span class="nf">eql?</span><span class="p">(</span><span class="mf">1.0</span><span class="p">,</span> <span class="o">&</span><span class="ss">:itself</span><span class="p">)</span>
<span class="c1"># => true</span>
<span class="mi">1</span><span class="p">.</span><span class="nf">eql?</span><span class="p">(</span><span class="mf">1.0</span><span class="p">)</span>
<span class="c1"># => false</span>
</code></pre>
<p>Thanks, gogo.</p> Ruby master - Feature #10474 (Open): [PATCH 1/1] Refactoring math.c (Combined some macros into on...https://bugs.ruby-lang.org/issues/104742014-11-04T05:47:06Zgogotanaka (Kazuki Tanaka)mail@tanakakazuki.com
<p>Hi, there.</p>
<p>We call <code>Need_Float</code> macro with <code>RFLOAT_VALUE</code> macro at the all time,<br>
so we don't need to assign result of <code>rb_to_float(x)</code> to temporary variable.</p>
<p>This is main reason I combined these two macros into one macro called <code>Get_RFloat_Value</code> and<br>
removed <code>Need_Float2</code>.</p>
<p>I'm not sure the name <code>Get_RFloat_Value</code> is best.<br>
If you have more preferable one, let me know and I'll fix patch :)</p>
<p>Thanks. gogo.</p> Ruby master - Feature #10426 (Open): A predicate to express congruencehttps://bugs.ruby-lang.org/issues/104262014-10-26T00:55:20Zsawa (Tsuyoshi Sawada)
<p>I occasionally felt the necessity of a predicate that checks congruence with respect to some operations:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">class</span> <span class="nc">Object</span>
<span class="k">def</span> <span class="nf">equal_by?</span> <span class="n">other</span><span class="p">,</span> <span class="o">&</span><span class="n">pr</span>
<span class="n">pr</span><span class="p">.</span><span class="nf">call</span><span class="p">(</span><span class="nb">self</span><span class="p">)</span> <span class="o">==</span> <span class="n">pr</span><span class="p">.</span><span class="nf">call</span><span class="p">(</span><span class="n">other</span><span class="p">)</span>
<span class="k">end</span>
<span class="k">alias</span> <span class="n">congruent?</span> <span class="n">equal_by?</span>
<span class="k">end</span>
<span class="mi">5</span><span class="p">.</span><span class="nf">congruent?</span><span class="p">(</span><span class="mi">2</span><span class="p">){</span><span class="o">|</span><span class="n">e</span><span class="o">|</span> <span class="n">e</span> <span class="o">%</span> <span class="mi">3</span><span class="p">}</span> <span class="c1">#=> true</span>
<span class="s2">"HELLO"</span><span class="p">.</span><span class="nf">equal_by?</span><span class="p">(</span><span class="s2">"Hello"</span><span class="p">,</span> <span class="o">&</span><span class="ss">:downcase</span><span class="p">)</span> <span class="c1">#=> true</span>
</code></pre> Ruby master - Feature #10425 (Open): A predicate method to tell if a number is near anotherhttps://bugs.ruby-lang.org/issues/104252014-10-26T00:38:58Zsawa (Tsuyoshi Sawada)
<p>A method <code>near?</code> like the following would be useful.</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">class</span> <span class="nc">Numeric</span>
<span class="k">def</span> <span class="nf">near?</span> <span class="n">other</span><span class="p">,</span> <span class="ss">delta: </span><span class="no">Float</span><span class="o">::</span><span class="no">EPSILON</span>
<span class="p">(</span><span class="n">other</span><span class="p">.</span><span class="nf">to_f</span> <span class="o">-</span> <span class="n">to_f</span><span class="p">).</span><span class="nf">abs</span> <span class="o"><=</span> <span class="n">delta</span><span class="p">.</span><span class="nf">to_f</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="k">class</span> <span class="nc">Time</span>
<span class="k">def</span> <span class="nf">near?</span> <span class="n">other</span><span class="p">,</span> <span class="ss">delta: </span><span class="no">Float</span><span class="o">::</span><span class="no">EPSILON</span>
<span class="n">to_f</span><span class="p">.</span><span class="nf">near?</span><span class="p">(</span><span class="n">other</span><span class="p">.</span><span class="nf">to_f</span><span class="p">,</span> <span class="ss">delta: </span><span class="n">delta</span><span class="p">)</span>
<span class="k">end</span>
<span class="k">end</span>
</code></pre>
<p>It can be used to check errors, or whether something is around something.</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="mf">23.24324</span><span class="p">.</span><span class="nf">near?</span><span class="p">(</span><span class="mf">23.23</span><span class="p">,</span> <span class="ss">delta: </span><span class="mf">0.5</span><span class="p">)</span> <span class="c1"># => true</span>
<span class="n">t1</span> <span class="o">=</span> <span class="n">t2</span> <span class="o">=</span> <span class="no">Time</span><span class="p">.</span><span class="nf">now</span>
<span class="n">t3</span> <span class="o">=</span> <span class="no">Time</span><span class="p">.</span><span class="nf">now</span>
<span class="n">t1</span><span class="p">.</span><span class="nf">near?</span><span class="p">(</span><span class="n">t2</span><span class="p">)</span> <span class="c1">#=> true</span>
<span class="n">t1</span><span class="p">.</span><span class="nf">near?</span><span class="p">(</span><span class="n">t3</span><span class="p">)</span> <span class="c1">#=> false</span>
<span class="mi">5</span><span class="p">.</span><span class="nf">near?</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="ss">delta: </span><span class="mi">1</span><span class="p">)</span> <span class="c1">#=> true</span>
</code></pre>
<p>Some testing frameworks have something similar to this, but I think this is an elementary concept that Ruby should support at it core.</p> Ruby master - Misc #10424 (Open): Error message when sorting NaNhttps://bugs.ruby-lang.org/issues/104242014-10-24T21:24:52Zjmthomas (Jason Thomas)
<p>When sorting an array of floats with a NaN you get a very confusing message:<br>
irb(main):001:0> [0.0/0.0,1.0,2.0].sort<br>
ArgumentError: comparison of Float with Float failed</p>
<p>Sorting a nil is much friendlier:<br>
irb(main):012:0> [nil,1.0,2.0].sort<br>
ArgumentError: comparison of NilClass with Float failed</p>
<p>This is confusing for many. Simply google for "comparison of Float with Float failed" and makes for a difficult debugging session for anyone who doesn't know that NaN produces this result. What I would expect is:<br>
irb(main):001:0> [0.0/0.0,1.0,2.0].sort<br>
ArgumentError: comparison of NaN with Float failed</p> Ruby master - Feature #10391 (Open): Provide %eISO-8859-1'string \xAA literal' string literals wi...https://bugs.ruby-lang.org/issues/103912014-10-16T04:08:31Zduerst (Martin Dürst)duerst@it.aoyama.ac.jp
<p>There is occasionally a need to use a string literal with an Encoding different from the source encoding.<br>
This proposes to use %e (e for encoding) to introduce such string literals.</p>
<p>The syntax used in the subject relies on the fact that the set of characters used in Encoding names and the set of characters used to surround the actual string in a %-literal are completely disjoint (or if they currently aren't, can be made completely disjoint). Alternatives would be to use % as a separator before and/or after the encoding, e.g. like this:</p>
<ul>
<li>%eISO-8859-1'string \xAA literal' # original proposal</li>
<li>%e%ISO-8859-1%'string \xAA literal' # before and after</li>
<li>%e%ISO-8859-1'string \xAA literal' # before only</li>
<li>%eISO-8859-1%'string \xAA literal' # after only</li>
<li>%e(ISO-8859-1)(string \xAA literal) # surrounding the encoding name</li>
</ul>
<p>The most frequent use of this would be with binary, so we probably want to allow a shortcut for binary, e.g.</p>
<ul>
<li>%eB'binary \x80 string'<br>
or even just</li>
<li>%b'binary \x08 string'<br>
We could then in the long term deprecate String#b, and go back to check string validity at creation.</li>
</ul>
<p>The upper/lowercase distinction can be used to distinguish single-quoted strings (%e) and double-quoted strings (%E). We probably also want something for regular expressions, but I'm not sure which letter is best.</p>
<p>There is one question about semantics: What's the meaning of e.g. %eGB2312'松本' in a program with a source encoding of UTF-8 or Shift_JIS? In some cases, it might be convenient to have the result contain the same characters. But that would mean that the data needs to be transcoded, and that could fail. The easier way to define this is that the result is the same as '松本'.force_encoding('GB2312'), i.e. just using the byte values.</p> Ruby master - Feature #10386 (Open): [PATCH 3/3] There is little possibility of using m_sqrt at c...https://bugs.ruby-lang.org/issues/103862014-10-14T18:18:44Zgogotanaka (Kazuki Tanaka)mail@tanakakazuki.com
<p>As far as I research, I couldn't find the reason we should keep m_sqrt function at complex.c.</p>
<p>So I've removed this and related code.</p>
<p>If you could know why we've keep it and toll me, I would appreciate it!</p> Ruby master - Feature #10366 (Open): New inspection form for rationalhttps://bugs.ruby-lang.org/issues/103662014-10-11T00:50:15Zsawa (Tsuyoshi Sawada)
<p>Currently, rational inspection is expressed with parentheses:</p>
<pre><code>(2/51)
</code></pre>
<p>If this were taken as a Ruby expression, it would mean integer division, whose value is <code>0</code> in this case. It does not make much sense to express that it is indeed not integer division and that it is rational by using parentheses. Now that we have rational literal using <code>r</code>, we can make the inspection form as:</p>
<pre><code>2/51r
</code></pre>
<p>This would be much less confusing.</p> Ruby master - Feature #10332 (Open): Rational literal for mixed fractionshttps://bugs.ruby-lang.org/issues/103322014-10-06T14:55:48Zsawa (Tsuyoshi Sawada)
<p>Current behavior of rational literal and <code>String#to_r</code> does not recognize mixed fractions. Mixed fraction is not rare, and is frequently used in places such as US length measurement.</p>
<p><img src="http://www.strongtie.com/graphics/anchorsystems/catalog/tables/217b-2012.gif" alt="drill bits"></p>
<p>I propose that rational literal and <code>String#to_r</code> should be extended to handle mixed fractions. Perhaps something like:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="mi">8</span> <span class="mi">1</span><span class="o">/</span><span class="mi">2</span><span class="n">r</span> <span class="c1">#=> (17/2)</span>
<span class="s2">"8 1/2"</span><span class="p">.</span><span class="nf">to_r</span> <span class="c1">#=> (17/2)</span>
</code></pre>
<p>or</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="p">(</span><span class="mi">8</span> <span class="mi">1</span><span class="o">/</span><span class="mi">2</span><span class="p">)</span><span class="n">r</span> <span class="c1">#=> (17/2)</span>
</code></pre> Ruby master - Feature #10331 (Open): String#to_r to recognize negative denominatorshttps://bugs.ruby-lang.org/issues/103312014-10-06T14:44:18Zsawa (Tsuyoshi Sawada)
<p>Current behavior or <code>String#to_r</code> does not recognize negative denominators. This can lead to confusing results:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="s2">"1/-3"</span><span class="p">.</span><span class="nf">to_r</span>
<span class="c1">#=> (1/1)</span>
</code></pre>
<p>I propose negative denominators to be recognized.</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="s2">"1/-3"</span><span class="p">.</span><span class="nf">to_r</span>
<span class="c1">#=> (-1/3)</span>
</code></pre> Ruby master - Feature #10327 (Open): Bool/False/True module for '==='https://bugs.ruby-lang.org/issues/103272014-10-05T16:09:17Zarimay (yasuhiro arima)
<p>ruby-core:0237 から始まる Boolean Class の話題で Boolean Module が提案されていました。<br>
それは必要とされているのか?という疑問とともに .true? メソッドなどが議論されていました。</p>
<p>それを読んで、=== メソッドを利用する Bool, False, True Module を書いてみました。<br>
これを使うと case 式で when TrueClass, FalseClass ではなく when Bool と短く書けます。<br>
また、case 式で 偽は when nil, false ではなく when False と書けます。<br>
そして、真は else ではなく when True と書けます。<br>
Bool, False, True という名前にしたのは、短く書きたいことと、リファレンスマニュアルでも使われているためです。</p>
<p>以下の ruby コードとほぼ同じ内容を C で書いた patch を添えます。</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">module</span> <span class="nn">Bool</span>
<span class="k">def</span> <span class="nc">self</span><span class="o">.</span><span class="nf">append_features</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
<span class="k">if</span> <span class="p">((</span><span class="n">m</span> <span class="o">!=</span> <span class="no">FalseClass</span><span class="p">)</span> <span class="ow">and</span> <span class="p">(</span><span class="n">m</span> <span class="o">!=</span> <span class="no">TrueClass</span><span class="p">))</span>
<span class="k">raise</span> <span class="no">TypeError</span><span class="p">,</span> <span class="s2">"`</span><span class="si">#{</span><span class="n">m</span><span class="p">.</span><span class="nf">name</span><span class="si">}</span><span class="s2">' isn't FalseClass nor TrueClass"</span><span class="p">,</span> <span class="nb">caller</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
<span class="k">end</span>
<span class="k">super</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="k">class</span> <span class="nc">FalseClass</span>
<span class="kp">include</span> <span class="no">Bool</span>
<span class="k">end</span>
<span class="k">class</span> <span class="nc">TrueClass</span>
<span class="kp">include</span> <span class="no">Bool</span>
<span class="k">end</span>
<span class="k">module</span> <span class="nn">False</span>
<span class="k">def</span> <span class="nc">self</span><span class="o">.</span><span class="nf">===</span><span class="p">(</span><span class="n">other</span><span class="p">)</span>
<span class="n">other</span><span class="p">.</span><span class="nf">nil?</span> <span class="o">||</span> <span class="n">other</span> <span class="o">==</span> <span class="kp">false</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="k">module</span> <span class="nn">True</span>
<span class="k">def</span> <span class="nc">self</span><span class="o">.</span><span class="nf">===</span><span class="p">(</span><span class="n">other</span><span class="p">)</span>
<span class="o">!</span><span class="n">other</span><span class="p">.</span><span class="nf">nil?</span> <span class="o">&&</span> <span class="n">other</span> <span class="o">!=</span> <span class="kp">false</span>
<span class="k">end</span>
<span class="k">end</span>
</code></pre>
<p>以下が実行例です。</p>
<pre><code>$ ./miniruby -e 'class String; include Bool; end;'
-e:1:in `append_features': 'String' isn't FalseClass nor TrueClass (TypeError)
from -e:1:in `include'
from -e:1:in `<class:String>'
from -e:1:in `<main>'
$ ./miniruby -e '[nil, false, true, 0].each {|obj| p obj.class.ancestors}'
[NilClass, Object, Kernel, BasicObject]
[FalseClass, Bool, Object, Kernel, BasicObject]
[TrueClass, Bool, Object, Kernel, BasicObject]
[Fixnum, Integer, Numeric, Comparable, Object, Kernel, BasicObject]
$ ./miniruby -e '[nil, false, true, 0].each {|obj|
case obj
when Bool; p [obj, Bool]
else p [obj, "-"]
end
}'
[nil, "-"]
[false, Bool]
[true, Bool]
[0, "-"]
$ ./miniruby -e '[nil, false, true, 0].each {|obj|
case obj
when False; p [obj, false]
when True; p [obj, true]
else p [obj, "-"]
end
}'
[nil, false]
[false, false]
[true, true]
[0, true]
</code></pre> Ruby master - Feature #10308 (Open): Pipes in Rubyhttps://bugs.ruby-lang.org/issues/103082014-09-30T22:06:38Zbaweaver (Brandon Weaver)keystonelemur@gmail.com
<p>Much akin to the Unix and Elixir piping, I think it would be an interesting feature to consider in Ruby.</p>
<p>There have already been a number of hack implementations of it:<br>
<a href="https://gist.github.com/pcreux/2f87847e5e4aad37db02" class="external">https://gist.github.com/pcreux/2f87847e5e4aad37db02</a><br>
<a href="https://github.com/banister/funkify" class="external">https://github.com/banister/funkify</a><br>
<a href="https://github.com/baweaver/streamable" class="external">https://github.com/baweaver/streamable</a></p> Ruby master - Feature #10305 (Open): Method for resolving all autoload statements / Add warning o...https://bugs.ruby-lang.org/issues/103052014-09-29T14:05:51ZQuintus (Marvin Gülker)post+rubybugs@guelker.eu
<p>Hi everyone,</p>
<p>I’m currently trying to make my way to create a chrooted process using Dir.chroot. However, once these lines are executed:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="no">Dir</span><span class="p">.</span><span class="nf">chroot</span><span class="p">(</span><span class="s2">"/some/path/here"</span><span class="p">)</span>
<span class="no">Dir</span><span class="p">.</span><span class="nf">chdir</span><span class="p">(</span><span class="s2">"/"</span><span class="p">)</span>
</code></pre>
<p>all libraries that use the #autoload method (which are quite a lot) fail with LoadErrors everywhere. This is natural, because after being locked into the chroot the referenced paths are simply gone. I could live with that if there was a method to load all the files referenced by #autoload immediately, which I would then simply call before I lock the process into the chroot. However, it appears there is not even a way to get a list of all constants that are to be autoloaded; the #autoload? method only allows checks for specific constants I simply do not have at hand without digging through all the library code, which is infeasable.</p>
<p>Therefore, I suggest do add one of the following methods to Ruby:</p>
<ol>
<li>Kernel#load_autoloads that loads all modules referenced with #autoload right now.</li>
<li>Kernel#autoload_list that returns a list of symbols for the constants that are to be autoloaded.</li>
</ol>
<p>The methods could probably also be on Module rather than on the Kernel module, but I guess this is topic for discussion.</p>
<p>I also suggest that when calling Dir::chroot() and there are constants that are marked as autoloadable, a warning should be printed that the referenced files cannot be loaded if the constants are ever used.</p>
<p>Steps to reproduce the LoadError problem after chroot:</p>
<p>Create a file "foo.rb" with this content:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="nb">autoload</span> <span class="ss">:Bar</span><span class="p">,</span> <span class="no">File</span><span class="p">.</span><span class="nf">join</span><span class="p">(</span><span class="no">Dir</span><span class="p">.</span><span class="nf">pwd</span><span class="p">,</span> <span class="s2">"bar.rb"</span><span class="p">)</span>
<span class="no">Dir</span><span class="p">.</span><span class="nf">chroot</span> <span class="s2">"/var/empty"</span>
<span class="no">Dir</span><span class="p">.</span><span class="nf">chdir</span> <span class="s2">"/"</span>
<span class="no">Bar</span><span class="p">.</span><span class="nf">new</span>
</code></pre>
<p>Create a file "bar.rb" with this content:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">class</span> <span class="nc">Bar</span>
<span class="k">end</span>
</code></pre>
<p>Ensure you have the directory "/var/empty", and it is empty.</p>
<p>Execute "foo.rb" with root rights.</p>
<pre><code>$ sudo ruby foo.rb
foo.rb:6:in `<main>': cannot load such file -- /home/quintus/foo/bar.rb (LoadError)
</code></pre>
<p>Valete,<br>
Marvin</p> Ruby master - Feature #10255 (Open): Math.log: check domain of base argumenthttps://bugs.ruby-lang.org/issues/102552014-09-18T05:58:47Zgogotanaka (Kazuki Tanaka)mail@tanakakazuki.com
<p>I suppose Math.log with base 0 or 1 is undefinable or has no meaning, so I think it should be raise <code>Math::DomainError</code></p>
<p>It is helpful to prevent unnecessary concern or misunderstand.</p>
<p>Yes I know there are many controversial issue about that, specially treating Math.log(0.0, 0.0), Math.log(1.0, 0.0) and Math.log(1.0, 1.0).</p>
<p>Even if there are some reason it had better return value,</p>
<p>I suppose <code>Math.log(2.0,1.0)</code> should not be <code>Infinity</code> and <code>Math.log(1.0, 0.0)</code> should not be <code>-0.0</code>.</p>
<p>In terms of <code>raises Math::DomainError instead of returning NaN if the base is less than 0</code> at the (<a href="https://bugs.ruby-lang.org/issues/9797" class="external">https://bugs.ruby-lang.org/issues/9797</a>)</p>
<p>, this PATCH make sense.</p>
<p>But please feel free to point out my unsatisfactory points. (Actually this is first time to write c-lang thoroughly)</p>
<p>Thank you. gogo.</p>
<p>Before</p>
<pre><code># For all 0 <= n < 1
Math.log(n, 1.0)
=> -Infinity
Math.log(1.0, 1.0)
=> NaN
# For all n > 1
Math.log(n, 1.0)
=> Infinity
Math.log(0.0,0.0)
=> NaN
# For all 0 < n < 1
Math.log(n, 0.0)
=> 0.0
# For all n >= 1
Math.log(n, 0.0)
=> - 0.0
</code></pre>
<p>After</p>
<pre><code># For all n >= 0. Actually for all n <- R (a real number) because of checking domain of first argument
Math.log(n, 1.0)
Math::DomainError: Numerical argument is out of domain - “log”
Math.log(n, 0.0)
Math::DomainError: Numerical argument is out of domain - “log”
</code></pre> Ruby master - Feature #10215 (Open): prohibit subclassing for classes without allocator in Rubyhttps://bugs.ruby-lang.org/issues/102152014-09-08T07:29:24ZHanmac (Hans Mackowiak)hanmac@gmx.de
<p>for the Ruby side it might be interesting if there is a way to prohibit the subclassing of classes that has no allocator like Symbol or Integer or TrueClass</p>
<p>but blocking it generally would break some C-Ext where it has base classes with undefined alloc</p> Ruby master - Feature #10181 (Open): New method File.openat()https://bugs.ruby-lang.org/issues/101812014-08-28T21:45:36Ztechnorama (Technorama Ltd.)
<p>The purpose of the openat() function is to enable opening files in directories other than the current working directory without exposure to race conditions. Any part of the path of a file could be changed in parallel to a call to open(), resulting in unspecified behavior. By opening a file descriptor for the target directory and using the openat() function it can be guaranteed that the opened file is located relative to the desired directory.</p>
<p>openat() is part of POSIX.1-2008.</p>
<p>Compatibility:<br>
Linux kernel >= 2.6.16<br>
FreeBSD >= 7.0<br>
OpenBSD >= 5.0<br>
NetBSD >= 6.1.4<br>
MacOS/X no</p>
<p>Pull request: <a href="https://github.com/ruby/ruby/pull/706" class="external">https://github.com/ruby/ruby/pull/706</a></p> Ruby master - Feature #10179 (Open): Net::HTTP::Get.new("https://google.com").basic_auth(user_nam...https://bugs.ruby-lang.org/issues/101792014-08-28T02:39:23Ztaterbase (George Shank)shankga@gmail.com
<p>Alternatively Net::HTTP could just detect https urls and do this for you.</p>
<p>I suggest:<br>
raise Exception("Accessing https without ssl\n url:#{URL}\n Please use http.use_ssl = true")</p> Ruby master - Feature #10177 (Open): Hash#has_key? and Hash#has_value? should be deprecatedhttps://bugs.ruby-lang.org/issues/101772014-08-27T22:57:16Zgogotanaka (Kazuki Tanaka)mail@tanakakazuki.com
<p>I referred to this Matz's remark.</p>
<p><a href="http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/43765" class="external">http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/43765</a></p>
<p>And I agree with him, I supposed <code>Hash#has_key?</code> should be preferred over <code>Hash#key?</code>,</p>
<p>so I replaced all of <code>Hash#has_key?</code> and <code>Hash#has_value?</code> in <code>lib/*</code> with <code>Hash#key?</code> and <code>Hash#value?</code></p> Ruby master - Feature #10176 (Open): Document how to perform net/http calls in parallelhttps://bugs.ruby-lang.org/issues/101762014-08-27T22:46:47Zmperham (Mike Perham)mperham@gmail.com
<p>The typhoeus gem is a big source of Ruby VM crashes. It seems to me that the only reason people use this gem is because net/http doesn't document how to GET many URLs in parallel. I'd like to see a simple code example in the net/http RDoc which show people how to fetch many URLs in parallel using net/http. For example, this might be one way but I don't know if it's the recommended or most efficient way:</p>
<p>urls = %w(<a href="http://www.yahoo.com" class="external">www.yahoo.com</a> <a href="http://www.google.com" class="external">www.google.com</a> <a href="http://www.bing.com" class="external">www.bing.com</a>)<br>
results = urls.map { |url| Thread.new { Net::HTTP.get(url, '/') } }.map(&:join)</p> Ruby master - Feature #10175 (Open): There’s no reason to prefer Proc.new over Kernel#proc anymorehttps://bugs.ruby-lang.org/issues/101752014-08-27T19:52:59Zgogotanaka (Kazuki Tanaka)mail@tanakakazuki.com
<p>We could’t use <code>Kernel#proc</code> if someone wanted to write code in the same way on both Ruby 1.8 and Ruby 1.9.<br>
Now that maintenance of Ruby versions 1.8 end, there’s no reason to prefer <code>Proc.new</code> over <code>Kernel#proc</code> anymore.</p>
<p><a href="https://www.ruby-lang.org/en/news/2014/07/01/eol-for-1-8-7-and-1-9-2/" class="external">https://www.ruby-lang.org/en/news/2014/07/01/eol-for-1-8-7-and-1-9-2/</a></p>
<p>I have fixed some coding style on the way</p> Ruby master - Feature #10152 (Open): String#strip doesn't remove non-breaking spacehttps://bugs.ruby-lang.org/issues/101522014-08-19T15:20:37ZAnonymous
<p>String#strip doesn't remove non-breaking space.</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="mf">2.1</span><span class="o">.</span><span class="mi">2</span> <span class="p">:</span><span class="mo">001</span> <span class="o">></span> <span class="s2">"</span><span class="se">\u</span><span class="s2">00A0"</span><span class="p">.</span><span class="nf">strip</span>
<span class="o">=></span> <span class="s2">" "</span>
</code></pre> Ruby master - Feature #10051 (Open): nbsp isn't remove with trimhttps://bugs.ruby-lang.org/issues/100512014-07-17T06:40:31Zbasvodde (Bas Vodde)basv@odd-e.com
<p>When I have a nbsp character in a string and call strip on it, it will not remove it even though is would could as a whitespace.</p>
<p>E.g.</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="no">Nokogiri</span><span class="o">.</span><span class="no">HTML</span><span class="p">(</span><span class="s2">"&nbsp;"</span><span class="p">).</span><span class="nf">text</span><span class="p">.</span><span class="nf">strip</span>
</code></pre>
<p>results in <code>" "</code> and should result in <code>""</code></p> Ruby master - Feature #10000 (Open): format width and precision with symbol hashhttps://bugs.ruby-lang.org/issues/100002014-06-30T23:24:04Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<p><code>Kernel#sprintf</code>では位置指定の場合には<code>*</code>によって引数での幅や精度の指定ができますが、<code>%{}</code>や<code>%<></code>でのSymbolの場合では指定できません。<br>
matz案では、<code>*</code>が指定された場合は該当する引数をArrayとみなして対応する幅、精度、値に分解するのはどうか、ということです。</p> Ruby master - Feature #9953 (Open): set_trace_func values which could be frozen or symbolshttps://bugs.ruby-lang.org/issues/99532014-06-18T15:34:51Zenebo (Thomas Enebo)tom.enebo@gmail.com
<p>I have been changing JRuby's tracing support internally and notice we churn through lots of extra Ruby objects per set_trace_func call. It strikes me that a couple of these values do not need to be allocated more than once. I propose:</p>
<ol>
<li>event_type becomes a symbol (or could be single frozen string per distinct event type)</li>
<li>filename becomes a single frozen string per file (frozen per spec and possible single instance as impl-specific detail)</li>
</ol>
<p>I find the value of these two values being mutable strings to be minimal and tracing speed will have less GC-pressure.</p> Ruby master - Feature #9929 (Open): add with_default method to Hashhttps://bugs.ruby-lang.org/issues/99292014-06-11T00:08:12Zdontfidget (Andrew Brown)andrew@dontfidget.com
<p>I would like to be able to write this:</p>
<pre><code>people.group_by(:first_name).with_default { [] }
</code></pre>
<p>instead of:</p>
<pre><code>hash = people.group_by(:first_name)
hash.default_proc = proc { [] }
</code></pre>
<p>or, perhaps:</p>
<pre><code>people.group_by(:first_name).tap { |hash| hash.default_proc = proc { [] } }
</code></pre>
<p>Just wondering if anyone else would be interested in a change like this. I posted a pull request in Rails for an ActiveSupport core extension that does the same thing but would love to see it in Ruby proper. (Pull request is at <a href="https://github.com/rails/rails/pull/15626" class="external">https://github.com/rails/rails/pull/15626</a>)</p> Ruby master - Feature #9918 (Open): Exception#cause should be shown in output and #inspecthttps://bugs.ruby-lang.org/issues/99182014-06-07T12:58:11ZEregon (Benoit Daloze)
<p>Exception#cause has been added in Feature <a class="issue tracker-2 status-5 priority-4 priority-default closed" title="Feature: Exception#cause to carry originating exception along with new one (Closed)" href="https://bugs.ruby-lang.org/issues/8257">#8257</a>.<br>
But the cause is never shown, one needs to inspect exc.cause to know the cause.</p>
<p>The cause should be shown by default, for instance:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">begin</span>
<span class="k">raise</span> <span class="s2">"cause"</span>
<span class="k">rescue</span>
<span class="k">raise</span> <span class="s2">"wrapper"</span>
<span class="k">end</span>
</code></pre>
<p>Should output:</p>
<pre><code>-:4:in `rescue in <main>': wrapper (RuntimeError)
from -:1:in `<main>'
Caused by:
-e:1:in `<main>': org (RuntimeError)
</code></pre>
<p>And not only the first 2 lines.<br>
Logically, #inspect should also include the cause such as:</p>
<pre><code>#<RuntimeError: wrapper cause:#<RuntimeError: org>>
</code></pre>
<p>What do you think?</p> Ruby master - Feature #9909 (Open): why shouldn't constant lookup check the nesting of module's namehttps://bugs.ruby-lang.org/issues/99092014-06-06T14:43:18Zrits (First Last)
<p>module A<br>
module B<br>
end<br>
end</p>
<p>above, B will have access to A's constants<br>
but below, it won't</p>
<p>module A::B<br>
end</p>
<p>is there a reason why the nesting of the name should not be part of the constant resolution algorithm?</p>
<p>when adding or reopening (deeply) nested modules/classes the :: syntax would be preferable, but you lose constant resolution</p> Ruby master - Feature #9887 (Open): Add uninclude pleasehttps://bugs.ruby-lang.org/issues/98872014-05-31T02:06:14Zshevegen (Robert A. Heiler)shevegen@gmail.com
<p>Hi!</p>
<p>I would like to have a way to cleanly uninclude a module.</p>
<p>Consider:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">module</span> <span class="nn">Foo</span>
<span class="no">TEST</span> <span class="o">=</span> <span class="s1">'123'</span>
<span class="k">end</span>
<span class="k">class</span> <span class="nc">Bar</span>
<span class="kp">include</span> <span class="no">Foo</span>
<span class="k">end</span>
</code></pre>
<p>Now I would like to uninclude Foo. I can remove constants and methods already,<br>
i.e. via <code>remove_method</code> or <code>undef</code>* and constants can be removed as well but<br>
I would like to have something like:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">class</span> <span class="nc">Bar</span>
<span class="n">uninclude</span> <span class="no">Foo</span>
<span class="k">end</span>
</code></pre>
<p>or</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="no">Bar</span><span class="p">.</span><span class="nf">uninclude</span> <span class="no">Foo</span>
<span class="no">Bar</span><span class="p">.</span><span class="nf">remove_ancestors</span> <span class="no">Foo</span>
</code></pre>
<p>Would this be possible? It might be useful but perhaps there is a<br>
reason why this can not be added.</p> Ruby master - Feature #9871 (Open): load a ruby library which doesn't have extensionhttps://bugs.ruby-lang.org/issues/98712014-05-28T10:07:07Znaruse (Yui NARUSE)naruse@airemix.jp
<p>拡張子のない Ruby スクリプトファイルを require する手段を提供しませんか。</p>
<p>Rubyスクリプトを拡張子無しで書くことはしばしばあります。<br>
例えば Unix のコマンドを Ruby で書くときがそうでしょう。</p>
<p>そのスクリプトの部品を将来再利用しそうなとき、場合によっては if $0 == <strong>FILE</strong> ハックを用いて、<br>
他のファイルから読み込まれたときはコマンドを起動を行わないようにするわけですが、<br>
拡張子がないとそもそも読み込めないので、まずファイル名を変えないといけません。</p>
<p>という具合で残念な感じなので、拡張子無しのファイルを読み込む手段を提供しませんか。<br>
require_relative は拡張子無しでも読める、辺りがいいと思うのですが。</p> Ruby master - Feature #9853 (Open): Please consider quoted generation of hash like in %h( foo bar...https://bugs.ruby-lang.org/issues/98532014-05-19T16:43:49Zshevegen (Robert A. Heiler)shevegen@gmail.com
<p>Hi.</p>
<p>In Ruby we can do this:</p>
<p>%w( foo bar bee blaa ) # => ["foo", "bar", "bee", "blaa"]</p>
<p>Ok makes an array of strings.</p>
<p>We can do this:</p>
<p>%i( foo bar bee blaa ) # => [:foo, :bar, :bee, :blaa]</p>
<p>Ok, makes an array of symbols.</p>
<p>We can also use [] on class Hash to generate a hash:</p>
<p>Hash[*%w( foo bar bee blaa )] # => {"foo"=>"bar", "bee"=>"blaa"}</p>
<p>My question:</p>
<p>Would it be possible to add %h() ? It would create a hash:</p>
<p>%h( foo bar bee blaa ) # => {"foo"=>"bar", "bee"=>"blaa"}</p>
<p>And would raise if odd number of arguments would be supplied.</p> Ruby master - Misc #9832 (Open): better concurrency in threadshttps://bugs.ruby-lang.org/issues/98322014-05-12T12:31:12Zariveira (Alexandre Riveira)alexandre@objectdata.com.br
<p>My application runs on top of rainbows using workers with multi-threaded.<br>
I realized that in ruby running on linux (my kernel config is slackware, debian not work)<br>
not equal to distribute the processing threads.<br>
To test this I created the file that is attached test_thread_schedule.rb<br>
The more the final test result is between 20/21 seconds means he has distributed processing equally<br>
So when the road test with ruby 1.9.2 on linux it does not perform.<br>
This improved in ruby 1.9.3 and further in ruby 2.0.<br>
But it still is not the ideal</p>
<p>My tests:<br>
ruby 1.9.2p320 => not work<br>
ruby 1.9.3p545 => 68 secs<br>
ruby 2.0.0p451 => 29 secs<br>
ruby 2.1.2p95 => 29 secs</p>
<p>ruby without GVL workfine<br>
rubinius 2.2.6 => 21 secs<br>
jruby 1.7.12 => 21 secs</p>
<p>But if I apply taskset (uncomment line in the test file <code>taskset -c -p 2 #{Process.pid}</code>)<br>
the results are noticeably better especially in ruby 1.9.2</p>
<p>ruby 1.9.2p320 => 21 secs<br>
ruby 1.9.3p545 => 30 secs<br>
ruby 2.0.0p451 => 23 secs<br>
ruby 2.1.2p95 => 23 secs</p>
<p>This reflects directly in the application, if one thread is is using 100% cpu with rainbows application begins to degrade coming to answer the other threads 7-16 seconds based time passes. Taskset already applied it decreases considerably. The same test applied cougar, gets to be virtually no impact, but since taskset is applied to the process, follow my code</p>
<p>rainbows:</p>
<p>before_fork do |server, worker|<br>
<code>taskset -c -p 2 #{Process.pid}</code></p>
<p>puma:</p>
<p>on_worker_boot do |index|<br>
<code>taskset -c -p 2 #{Process.pid}</code></p>
<p>Could internally ruby linux treat in a special way so that the behavior of threads is improved ???<br>
If this behavior can not be improved through a new feature in ruby I am grateful for any help for<br>
fixing the process taskset on a particular processor which is not ideal.</p> Ruby master - Feature #9807 (Open): String.new with blockhttps://bugs.ruby-lang.org/issues/98072014-05-06T11:22:07Zcitizen428 (Michael Kohl)citizen428@gmail.com
<p>After a discussion in our team chat today, I wondered if it would be a good idea to have a version of String.new that accepts a block and works as a string builder. Something like</p>
<pre><code>string = String.new("foo") do |s|
s << "bar"
end
string #=> "foobar"
</code></pre>
<p>If the argument is omitted an empty string would be passed to the block instead.</p>
<p>That could be a nice solution to avoid all the Array#join and "".tap hacks for string creation.</p> Ruby master - Feature #9784 (Open): Alias URI#merge to URI#joinhttps://bugs.ruby-lang.org/issues/97842014-04-29T05:42:45Zplexus (Arne Brasseur)arne@arnebrasseur.net
<p>To make Pathname and URI more consistent, it would be great to have a <code>join</code> instance method that behaves like <code>Pathname#join</code>. This could be a simple alias of <code>URI#merge</code>.</p> Ruby master - Feature #9725 (Open): Do not inspect NameError target object unless verbosehttps://bugs.ruby-lang.org/issues/97252014-04-10T22:19:37Zheadius (Charles Nutter)headius@headius.com
<p>At least once every few months, we get an error report of JRuby raising a memory error where MRI does not due to <code>NameError</code>'s <code>Message</code> object holding a reference to an object that's too large to inspect. I propose that this inspection of the target object should only be done in verbose mode.</p>
<a name="Background"></a>
<h2 >Background:<a href="#Background" class="wiki-anchor">¶</a></h2>
<p><code>NameError</code> is raised when a variable-like method call fails to find a defined method. The resulting exception is created with a hidden <code>NameError::Message</code> that holds the object in which the method could not be found.</p>
<p>When name error needs to render its message, such as when it bubbles out or when <code>#message</code> is called, it does <code>to_str</code> on the <code>NameError::Message</code>, which ends up inspecting the target object. If this object's inspect output is large (or infinite) it can end up consuming a large amount of memory.</p>
<a name="Problems"></a>
<h2 >Problems:<a href="#Problems" class="wiki-anchor">¶</a></h2>
<ul>
<li>If the amount of memory required to render a <code>NameError</code> exceeds available memory, a very confusing and misleading memory error can be raised instead.</li>
<li>If the target object is considered sensitive data, it will end up bubbling out through potentially untrustworthy code. It is an encapsulation flaw, basically.</li>
<li>A <code>NameError</code> that gets held in memory will also prevent GC of the object it references.</li>
</ul>
<a name="Solutions"></a>
<h2 >Solutions:<a href="#Solutions" class="wiki-anchor">¶</a></h2>
<ul>
<li>
<code>NameError</code> should not capture the target object.</li>
<li>
<code>NameError</code> should build a message based on the target object <em>at creation time</em>, and only include information useful to indicate the type of object.</li>
<li>(Optional) If verbose mode is set, <code>NameError</code> can just do what it does now.</li>
</ul> Ruby master - Misc #9724 (Open): Warnings in Ruby: allow per-file directives to i.e. suppress war...https://bugs.ruby-lang.org/issues/97242014-04-10T18:21:31Zshevegen (Robert A. Heiler)shevegen@gmail.com
<p>Hi,</p>
<p>A bit of intro.</p>
<p>I usually run all my ruby code with -w. I feel that it gives me some more security if the<br>
ruby parser does not have to think about ambiguous code.</p>
<p>Now this works perfect for my own code - I know what I have written, I know how to fix it,<br>
so my code runs fine.</p>
<p>Problem is other people who do not use the -w switch, and in doing so their stuff outputs<br>
a lot of warnings if I require their project and use them.</p>
<p>This is somewhat annoying and there is no real good way to fix it as far as I know.</p>
<p>Modifying $VERBOSE and setting it to nil is of no real help because it works globally.<br>
But I'd rather want something to be used on a per-file basis.</p>
<p>Would it be possible to enable something that could be used on a per file<br>
basis? Kernel.no_warnings, or Kernel.be_silent or something like this?</p> Ruby master - Feature #9667 (Open): Optimization of __FILE__ and __dir__https://bugs.ruby-lang.org/issues/96672014-03-24T09:19:04Zsawa (Tsuyoshi Sawada)
<p>In the same spirit as the string literal followed by <code>freeze</code> is optimized, I think <code>__FILE__</code> and <code>__dir__</code> should be optimized. Currently, they return different object id each time they are called.</p>
<pre><code>__FILE__.object_id # => 70183725179420
__FILE__.object_id # => 70183725129020
...
</code></pre>
<p>I propose them to be optimized so that they are only created once per occurrence.</p>
<pre><code>__FILE__.object_id # => 70183725179420
__FILE__.object_id # => 70183725179420
...
</code></pre> Ruby master - Feature #9613 (Open): Warn about unsafe ossl ciphershttps://bugs.ruby-lang.org/issues/96132014-03-08T03:04:48Zzzak (zzak _)
<p>As of r45274, we now have sane whitelist of available OpenSSL ciphers. However, this patch breaks backwards compatibility for any apps that use any ciphers not whitelisted.</p>
<a name="Solution"></a>
<h2 >Solution<a href="#Solution" class="wiki-anchor">¶</a></h2>
<ul>
<li>Implement a new class: OpenSSL::SSL::Ciphers
<ul>
<li>This class defines a constant for every whitelisted cipher used by DEFAULT_PARAMS[:ciphers]</li>
<li>Any constant not found within this class should raise a warning and report to the user</li>
</ul>
</li>
<li>Add an OpenSSL::SSL::Configuration class
<ul>
<li>Designed to default to no compression, and no sslv2/v3</li>
<li>Used by DEFAULT_PARAMS[:options]</li>
<li>This class may contain helper methods such as: #compression_enabled?</li>
</ul>
</li>
</ul>
<a name="Pros"></a>
<h2 >Pros<a href="#Pros" class="wiki-anchor">¶</a></h2>
<ul>
<li>We don't break anything, without warning users first</li>
<li>Maintaining future whitelist ciphers is easier</li>
<li>Future unsupported/blacklist ciphers are already dismissed</li>
<li>Users are able to extend cipher lists to support their needs (by adding a constant to OpenSSL::SSL::Ciphers)</li>
</ul>
<a name="Concerns"></a>
<h2 >Concerns<a href="#Concerns" class="wiki-anchor">¶</a></h2>
<p>I have discussed this with Martin, and we'd like to open up this discussion for feedback. We're particularly concerned about backporting r45274 as it breaks compatibility. We should also consider:</p>
<ul>
<li>Do we backport both patches or just the warning?</li>
<li>Should we bother backporting deprecation warnings?
<ul>
<li>Since r45274 is not a security fix, do we consider this a bug?</li>
<li>Rails only introduces deprecation notices in new minor releases (ie: Ruby-2.2.0)</li>
</ul>
</li>
<li>r45274 is a major change that could break existing apps, even considering security</li>
</ul> Ruby master - Feature #9585 (Open): Add Object#in? to make ruby easier to readhttps://bugs.ruby-lang.org/issues/95852014-03-01T11:43:17Zsowieso (So Wieso)sowieso@dukun.de
<p>Please add an in? method to all objects, that allows the following:<br>
<code>4.in? 1,2,3,4 4.in? 1..4 "a".in? "abc" 7.in? 0..2, 5..8 3.in? small_numbers.select(&:odd?) =>true</code><br>
Background:<br>
Since my first steps in ruby it always bugs me, that using Array#include? to perform multiple checks in one line breaks symmetry, forces me to read backward and thus lets me stumble in the head. Ruby tries to be close to natural language, and therefore the subject should stand on the left side. Example:<br>
<code>if status == 1 if status == 1 or status == 2 if [1,2,127].include? status # breaks symmetry if status.in? 1, 2, 127 # better</code></p>
<p>Pros:<br>
‣ Nicer to read, no need to read the line backward (brings joy to writers and readers)<br>
‣ No new keyword<br>
‣ Breaks nothing</p>
<p>Cons:<br>
‣ One more method in Object<br>
‣ <code>"a".in? "abc", "def" vs "a".in? ["abc", "def"]</code> (implementation is yet an example)</p>
<p>Neutral:<br>
‣ Yet one more way to do it (isn't that ruby-style?)<br>
‣ Belongs to Object, as a comparison operator like ==, eql?, ===, nil?<br>
‣ “only cosmetics” vs elegance</p>
<p>Implementation for testing (you'd certainly find a less naive implementation):<br>
<code>class Object def in? *args raise ArgumentError if args.empty? args.any? {|a| a == self or a.include? self rescue false} end end</code><br>
This is related to <a href="https://bugs.ruby-lang.org/issues/3845" class="external">https://bugs.ruby-lang.org/issues/3845</a><br>
which was rejected for being an operator (even though the keyword already existed)</p> Ruby master - Feature #9557 (Open): Enumerator#next and Enumerator#peek with argumenthttps://bugs.ruby-lang.org/issues/95572014-02-23T09:14:18Zsawa (Tsuyoshi Sawada)
<p>It often happens that I want to move the current index of an enumerator by some arbitrary number <code>n</code>. <code>Enumerator#feed</code> takes the element as the argument, but that cannot be used if the enumerator has duplicate elements, or when I do not have information of a particular element to choose but just want to increment the index by some number. <code>Enumerator#next</code>, on the other hand, has a fixed value <code>1</code> to be incremented. It would be convenient if <code>Enumerator#next</code> takes an optional argument that represents the difference of the index to be incremented. The argument can be understood to be defaulted to <code>1</code> when absent.</p>
<p>Also, I often want to look not necessarily the current position, but some position away. It would be good if <code>Enumerator#peek</code> takes an optional argument that represents the positional difference to be peeked. The argument can be understood to be defaulted to <code>0</code> when absent.</p>
<pre><code>enum = [0, 1, 2, 3, 4, 5, 6, 7, 8].to_enum
enum.peek # => 0
enum.peek(0) # => 0
enum.peek(1) # => 1
enum.peek # => 0
enum.next # => 0
enum.next(1) # => 1
enum.next(2) # => 2
enum.peek # => 4
enum.peek(0) # => 4
enum.peek(1) # => 5
enum.peek # => 4
enum.next # => 4
enum.next(1) # => 5
enum.next(2) # => 6
peek # => 8
</code></pre> Ruby master - Feature #9556 (Open): Add HTTP#get block functionality to HTTP.gethttps://bugs.ruby-lang.org/issues/95562014-02-23T05:53:38Zsrawlins (Sam Rawlins)sam.rawlins@gmail.com
<p>This is kind of an alternative to <a class="issue tracker-2 status-1 priority-4 priority-default" title="Feature: make Net::HTTP.get_print not only to $stdout but to an IO as a parameter (Open)" href="https://bugs.ruby-lang.org/issues/9527">#9527</a>.</p>
<p>HTTP#get can be passed a block. The shortcut method, HTTP.get, however, cannot. This patch adds such functionality.</p>
<p>This allows someone to be able to write the response of a GET, in fragments, without dealing directly with Net::HTTPResponse, like the request in <a class="issue tracker-2 status-1 priority-4 priority-default" title="Feature: make Net::HTTP.get_print not only to $stdout but to an IO as a parameter (Open)" href="https://bugs.ruby-lang.org/issues/9527">#9527</a>:</p>
<pre><code>File.open('result.zip', 'wb') {|f|
Net::HTTP.get('www.example.com', '/file.zip') do |chunk|
f.write chunk
end
}
</code></pre>
<p>This patch includes that example as documentation, and a test. It is attached, and available at: <a href="https://github.com/srawlins/ruby/compare/add-block-to-http-get" class="external">https://github.com/srawlins/ruby/compare/add-block-to-http-get</a></p> Ruby master - Feature #9553 (Open): Make argument validation routine of a method an objecthttps://bugs.ruby-lang.org/issues/95532014-02-22T07:02:45ZAnonymous
<p>Ruby methods have their acceptable argument configurations specified by special syntax. As argument fields has gotten more complex, simple <code>#arity</code> is no longer enough to capture the type of the argument field. I suggest making this argument validation routine (so far defined by special syntax) introspectible, and reusable for the purposes of metaprogramming. One possibility would be to have eg. <code>ArgumentValidator</code> and/or <code>UnboundArgumentValidator</code> objects just like we have method and unbound method objects. Then eg. <code>Method#argity</code> / <code>Proc#argity</code> would return that <code>ArgumentValidator</code> or <code>UnboundArgumentValidator</code>. The purpose of this post is not to suggest the final solution, but to bring attention to the problem of non-oo nature of built-in argument validation and the growing need to do something about that.</p> Ruby master - Feature #9527 (Open): make Net::HTTP.get_print not only to $stdout but to an IO as ...https://bugs.ruby-lang.org/issues/95272014-02-18T10:59:48Zbdimych (Dmitry Bolshakov)bdimych@narod.ru
<p>e.g.</p>
<p>url = '<a href="http://server/path/large-file.zip" class="external">http://server/path/large-file.zip</a>'<br>
fh = File.new(File.basename(url), 'wb')<br>
Net::HTTP.get_print(URI(url), fh)<br>
fh.close</p>
<p>imho looks good</p> Ruby master - Feature #9522 (Open): Float( "NaN" ), Float( "Infinity" )https://bugs.ruby-lang.org/issues/95222014-02-16T05:36:59ZAnonymous
<p>I have noticed that 0.0 / 0.0 returns NaN, but Float( "#{0.0 / 0.0}" ),<br>
or simply Float( "NaN" ) does not return Float::NAN, failing instead.<br>
To me, even inclusion of NaN among floats is somewhat questionable, but<br>
since it's there, Float( "NaN" ) needs to be discussed. Same would go<br>
for Float( "Infinity" ).</p>
<p>The issue here might be that not all the users are aware that NaN and<br>
Infinity have special meaning in the context of floats, and they might<br>
use these strings inadvertantly without actually meaning a Float.</p> Ruby master - Misc #9516 (Open): Consolidate all deprecation messages to one or more helper methodshttps://bugs.ruby-lang.org/issues/95162014-02-13T17:11:56Zenebo (Thomas Enebo)tom.enebo@gmail.com
<p>I was examining this blog entry: <a href="http://batsov.com/articles/2014/02/05/a-list-of-deprecated-stuff-in-ruby/" class="external">http://batsov.com/articles/2014/02/05/a-list-of-deprecated-stuff-in-ruby/</a> and I wanted to add these warning in JRuby. I thought it would be nice if I could make a higher level construct (e.g. @RubyDeprecated(use="Dir.exist?")) but then realized MRI does not consistently have the same warning string formats:</p>
<p>"Dir.exists? is a deprecated name, use Dir.exist? instead"<br>
"GDBM#index is deprecated; use GDBM#key"<br>
"Zlib::GzipReader#bytes is deprecated; use #each_byte instead"</p>
<p>Some helper methods could make these consistent and then I could make the higher level abstraction in JRuby as well. Since these are warnings I might still make an abstraction and let JRuby be a little inconsistent but I thought I would pass this idea along.</p> Ruby master - Feature #9445 (Open): Support emitting 1.9 Symbol keyword Hash syntax when pretty p...https://bugs.ruby-lang.org/issues/94452014-01-24T02:16:43Zpostmodern (Hal Brodigan)postmodern.mod3@gmail.com
<p>Ruby 1.9 first introduced support for keyword Hash syntax for simple Symbol keys. However, Hash#inspect and pp do not emit the same syntax when printing Symbol keys. This patch adds support for "foo: " syntax to the pp_hash method, iff the key is a Symbol and does not contain any spaces or special characters. While the patch is probably not perfect, I hope to start a discussion.</p> Ruby master - Feature #9174 (Open): value receiving block for Hash#has_key?https://bugs.ruby-lang.org/issues/91742013-11-29T06:33:11Zrits (First Last)
<p>Seems useful to be able to check for presence (as opposed to falsyness) and receive the value</p>
<p>class Hash<br>
def has_key?(key, &block)<br>
val = fetch(key){return false}<br>
block.(val) if block<br>
true<br>
end<br>
end</p>
<p>if h.has_key? :key do |val|</p>
<h1></h1>
<p>end; else</p>
<h1></h1>
<p>end</p> Ruby master - Feature #9111 (Open): Encoding-free String comparisonhttps://bugs.ruby-lang.org/issues/91112013-11-14T22:15:06Zsawa (Tsuyoshi Sawada)
<p>=begin<br>
Currently, strings with the same content but with different encodings count as different strings. This causes strange behaviour as below (noted in StackOverflow question <a href="http://stackoverflow.com/questions/19977788/strange-behavior-in-packed-ruby-strings#19978206" class="external">http://stackoverflow.com/questions/19977788/strange-behavior-in-packed-ruby-strings#19978206</a>):</p>
<pre><code>[128].pack("C") # => "\x80"
[128].pack("C") == "\x80" # => false
</code></pre>
<p>Since <code>[128].pack("C")</code> has the encoding ASCII-8BIT and <code>"\x80"</code> (by default) has the encoding UTF-8, the two strings are not equal.</p>
<p>Also, comparison of strings with different encodings may end up with a messy, unintended result.</p>
<p>I suggest that the comparison <code>String#<=></code> should not be based on the respective encoding of the strings, but all the strings should be internally converted to UTF-8 for the purpose of comparison.</p>
<p>=end</p> Ruby master - Feature #9095 (Open): Allow `Symbol#to_proc` to take argumentshttps://bugs.ruby-lang.org/issues/90952013-11-08T04:55:04Zalexeymuranov (Alexey Muranov)
<p>=begin<br>
After discussing <a class="issue tracker-2 status-7 priority-4 priority-default closed" title="Feature: New one-argument block syntax: &. (Feedback)" href="https://bugs.ruby-lang.org/issues/9076">#9076</a>, i've decided to propose this:</p>
<p>class Symbol<br>
def to_proc(*args)<br>
proc do |x|<br>
x.public_send(self, *args)<br>
end<br>
end<br>
end</p>
<p>p = :+.to_proc(1)</p>
<p>p[2] # => 3</p>
<p>[1, 2, 3, 4].map &:to_s.to_proc(2) #=> ["1", "10", "11", "100"]</p>
<p>This would allow constructing more kinds of procs without using literal blocks.<br>
=end</p> Ruby master - Feature #8967 (Open): add uninclude and unextend methodhttps://bugs.ruby-lang.org/issues/89672013-09-30T14:18:34Zwindwiny (wind winy)windwinyubt@gmail.com
<p>Can be add uninclude and unextend method to ruby core ?</p>
<p>That enable Klass include or prepend different module,<br>
make DECORATOR design pattern is easier.</p> Ruby master - Feature #8862 (Open): getoptlong to accept user-provided commandlinehttps://bugs.ruby-lang.org/issues/88622013-09-04T21:53:13Znoon (Fabrice Bauzac)libnoon@gmail.com
<p>=begin<br>
Hello,</p>
<p>The (({getoptlong})) module (({class GetoptLong})) provides no way for the user to provide a commandline that is not (({ARGV})). It appears that (({GetoptLong})) is tied to the (({ARGV})) global variable and this cannot be modified. The fine (({getoptlong})) algorithms are not reusable in particular situations where the command is not in (({ARGV})), short of performing some kind of hack to temporarily modify (({ARGV})).</p>
<p>This ticket requests the addition of a way to redirect a (({GetoptLong})) object to eat a different (({Array})) of (({String})) than (({ARGV})).</p>
<p>One way to implement this would be to change the getoptlong code to use (({@argv})) instead of (({ARGV})), set (({@argv})) to a copy of (({ARGV})) in (({#initialize})), and provide a (({#set_commandline(Array)})) method to set (({@argv})) to a copy of the argument.<br>
=end</p> Ruby master - Feature #8786 (Open): Process.clock_gettime(:realtime)https://bugs.ruby-lang.org/issues/87862013-08-15T22:33:13Zznz (Kazuhiro NISHIYAMA)
<p>Socket や IO#seek のように Process.clock_gettime の第一引数も Symbol で :realtime などを受け付けると良いのではないでしょうか。</p> Ruby master - Feature #8714 (Open): Non-interpolated regular expression literalhttps://bugs.ruby-lang.org/issues/87142013-08-01T08:07:19Zphluid61 (Matthew Kerwin)matthew@kerwin.net.au
<p>=begin</p>
<p>I propose a new %string for non-interpolated regexp literals: %R</p>
<p>It is common to see erroneous bug reports around the use of ((%#%)) in regexp literals, for example where (({/[$#]/})) raises a syntax error "unexpected $undefined", and this confuses people. The only solution is to rearrange the regular expression (such as (({/[$#]/}))), which is not always desirable.</p>
<p>An non-interpolated regexp, such as (({%R/[$#]/})), would allow a much simpler resolution.</p>
<p>=== Known Issues</p>
<ul>
<li>the capitalisation is the opposite of %Q(interpolated) and %q(uninterpolated)</li>
<li>%R was also proposed for literal Rationals in <a class="issue tracker-2 status-5 priority-4 priority-default closed" title="Feature: Rational number literal (Closed)" href="https://bugs.ruby-lang.org/issues/8430">#8430</a>, although I believe this has been superseded by the (({1.2r})) syntax</li>
</ul>
<p>=end</p> Ruby master - Feature #8688 (Open): #sprintf should accept strings as keyshttps://bugs.ruby-lang.org/issues/86882013-07-26T02:33:03ZQuintus (Marvin Gülker)post+rubybugs@guelker.eu
<p>=begin<br>
Hi there,</p>
<p>Kernel#sprintf should support strings as keys. Currently it only works with symbols:</p>
<p>{irb(main):001:0> str = "This is %{foo}."<br>
=> "This is %{foo}."<br>
irb(main):002:0> sprintf(str, :foo => "bar")<br>
=> "This is bar."<br>
irb(main):003:0> sprintf(str, "foo" => "bar")<br>
KeyError: key{foo} not found<br>
from (irb):3:in <code>sprintf' from (irb):3 from /opt/rubies/mri/bin/irb:12:in </code>'}</p>
<p>ruby -v: ruby 2.0.0p247 (2013-06-27 revision 41674) [x86_64-linux]</p>
<p>If I want a user to be able to enter format strings and (later) the corresponding parameter values, I don’t want to call #to_sym on the keys (because generating symbols from user input is known to cause memory issues).</p>
<p>Valete,<br>
Marvin</p>
<p>=end</p> Ruby master - Feature #8614 (Open): Object#singleton_class with a blockhttps://bugs.ruby-lang.org/issues/86142013-07-10T01:04:42Zsawa (Tsuyoshi Sawada)
<p>=begin<br>
Most of the time when I use <code>Object#singleton_class</code>, I use it with <code>class_eval</code> following it, like follows:</p>
<pre><code>class Foo
singleton_class.class_eval{attr_accessor :bar}
end
</code></pre>
<p>I think it would be convenient if <code>Object#singleton_class</code> can optionally take a block so that the following will mean the same as above.</p>
<pre><code>class Foo
singleton_class{attr_accessor :bar}
end
</code></pre>
<p>=end</p> Ruby master - Feature #8598 (Open): Expose information whether a timezone offset has been explici...https://bugs.ruby-lang.org/issues/85982013-07-03T20:27:06Zgpoul (Gerhard Poul)gerhard.poul@gmail.com
<p>For some applications it would be beneficial if Ruby would expose information on whether a DateTime object has an explicitly set timezone offset. An application might want to use DateTimes that don't concern themselves with timezone offsets and don't perform conversions between zones. To make this backwards compatible I'd suggest to expose a flag on whether an offset has been set or whether the default (offset=0) has been automatically assumed. An application might then decide to handle a DateTime that has the default timezone offset automatically assumed (offset=0) differently from a DateTime that has UTC (offset=0) set explicitly.</p> Ruby master - Feature #8566 (Open): [PATCH] Allow to configure additional preludeshttps://bugs.ruby-lang.org/issues/85662013-06-24T20:37:18Zvo.x (Vit Ondruch)v.ondruch@tiscali.cz
<p>Could you please accept the patch [1], which adds new '--with-prelude' configuration option. This configuration option allows to specify additional preludes and compile it into prelude.c, therefore execute the code on each start of Ruby.</p>
<p>= Rationale</p>
<p>In Fedora/RHEL/CentOS, there is ABRT tool [2], which allows to automatically report program failures. I developed abrt gem [3, 4], which adds support for catching of unhandled Ruby exceptions, this in turns help improve the quality of Fedora and software we ship.</p>
<p>Every Ruby programmer could require this gem and it would report the bugs, however, there is no way how to convince everybody, that they should use abrt gem. Therefore, it would make more sense, if the gem is automatically loaded by Ruby itself, when it is available on system. For that, I could patch Ruby, but I think somebody else could benefit from this feature as well, hence I am proposing this patch to upstream.</p>
<p>JFYI, Motohiro Kosaki once proposed to add ABRT support to Ruby [5], but I think it would be far better if Ruby knows nothing about ABRT by default and we could solve it on distribution level.</p>
<p>Thanks for considering.</p>
<p>[1] <a href="https://github.com/voxik/ruby/commit/efcca5238cf0804275e76d99a599190250d9dd0c" class="external">https://github.com/voxik/ruby/commit/efcca5238cf0804275e76d99a599190250d9dd0c</a><br>
[2] <a href="https://fedorahosted.org/abrt/" class="external">https://fedorahosted.org/abrt/</a><br>
[3] <a href="http://rubygems.org/gems/abrt" class="external">http://rubygems.org/gems/abrt</a><br>
[4] <a href="https://github.com/voxik/abrt-ruby" class="external">https://github.com/voxik/abrt-ruby</a><br>
[5] <a href="https://github.com/kosaki/ruby/commit/6283017dc2747f306808ce530292dc51273746ec" class="external">https://github.com/kosaki/ruby/commit/6283017dc2747f306808ce530292dc51273746ec</a></p> Ruby master - Feature #8564 (Open): Extend Module#attr... methodshttps://bugs.ruby-lang.org/issues/85642013-06-24T13:55:07ZAnonymous
<p>Extend #attr_reader, #attr_writer, #attr_accessor syntax to accept default values, such as:</p>
<pre><code>attr_reader foo: 42, bar: 43
</code></pre>
<p>Possibility of closures evaluated at initialization time might also be considered:</p>
<pre><code>attr_reader baz: -> { Time.now }, quux: 42
</code></pre> Ruby master - Feature #8494 (Open): Safe method for defensive copies. alternative to 'dup'https://bugs.ruby-lang.org/issues/84942013-06-05T16:26:27Zellipsoid (Andrew Northrop)andrew.northrop@gmail.com
<p>As a developer<br>
I want a simple method that will return a "safe" version of my object<br>
So I can make defensive copies easily and safely</p>
<p>This is related to issue 1844: <a href="http://www.ruby-forum.com/topic/3650325" class="external">http://www.ruby-forum.com/topic/3650325</a></p>
<p>I have tried using 'dup' to make defensive copies, but 'dup' throws an exception when used on a Fixnum and the like, because these values aren't copyable.</p>
<p>I tried to throw a "respond_to?" around the call, but that didn't work, since these classes still technically respond to 'dup'.</p>
<p>It looks like there's an extension method to do this (<a href="http://ruby-doc.org/gems/docs/c/core_ex-0.6.6.3/CoreEx/TryDup.html" class="external">http://ruby-doc.org/gems/docs/c/core_ex-0.6.6.3/CoreEx/TryDup.html</a>), but I don't see why this isn't in the core language. It seems like a common enough use case.</p>
<p>As far as naming, 'dup' is really just the means to an end. A more meaningful name would be something like 'defensive', or 'safe'. Although 'try_dup' like the extension method would also work.</p>
<p>Any chance of something like this making it in to Ruby core?</p> Ruby master - Feature #8452 (Open): Kernel#otherwise to rewrite code like (obj || default_obj).do...https://bugs.ruby-lang.org/issues/84522013-05-26T20:41:57Zprijutme4ty (Ilya Vorontsov)prijutme4ty@gmail.com
<p>Imagine you have code like this<br>
(long_computation_chain || []).do_smth<br>
While it isn't difficult, but it forces a developer to use additional parentheses. It can be difficult to trace where opening bracket is when computation_chain is long. Due to this idiom code cannot be read or written left to right. This is the same problem as for<br>
Hash[ *long_computation ]</p>
<p>So I propose a simple method Kernel#otherwise</p>
<pre><code>def otherwise(value, check = nil)
if !check
self || value
else
check.to_proc.call(self) ? self : value
end
end
</code></pre>
<p>So now one can write:<br>
long_computation_chain.otherwise([]).do_smth<br>
or<br>
long_computation_chain.otherwise('default', :empty?).do_smth<br>
or<br>
long_computation_chain.otherwise('numbers not allowed', ->(x){ x.grep(/\d+/) }).do_smth</p> Ruby master - Feature #8437 (Open): custom operators, unicodehttps://bugs.ruby-lang.org/issues/84372013-05-23T09:20:16Zeike.rb (Eike Dierks)eike@inter.net
<p>The ruby language does currently one support a predefined set of operators.<br>
It would be nice to add custom operators.<br>
A lot of people ask about the elvis operator aka (?:) to cope with nil, aka rails try()</p>
<p>This probably is a problem with the parser at first,<br>
because introducing new operators makes parsing a lot more complicated.</p>
<p>Maybe we could allow symbols from the unicode space<br>
to be used as new operators?<br>
That would be compatible with all before code written in ascii.</p>
<p>So we could allow all the symbols from the unicode Math operators plane<br>
to be available as operators in Ruby.</p>
<p>While few of you might have tried that,<br>
Unicode is fully available for naming identifiers.</p>
<p>We should also extend the set of operators to the unicode space.</p>
<p>While we are still not used to it now,<br>
to have some unicode characters in our codes,<br>
could really add to the expressivness.</p>
<p>So as of today, you can already name your variable like<br>
delta_t or Δt</p>
<p>Or if you're working with angles,<br>
you might call your variables α or Θ<br>
This is completely legal in Ruby.</p>
<p>I'm asking for:<br>
this_set ∪ other_set</p>
<p>or maybe we could even do this for prefix like:<br>
√2</p>
<p>I'd believe the math operator plane of unicode should be removed<br>
from the allowable names of identifiers,<br>
but should instead be reserved for operators in the parser, like + nowdays is.</p> Ruby master - Feature #8404 (Open): virtual, hooked or read only global variabels for ruby only c...https://bugs.ruby-lang.org/issues/84042013-05-14T21:19:29ZHanmac (Hans Mackowiak)hanmac@gmx.de
<p>currently pure virtual or hooked global variables can only be defined with cFunctions like rb_define_virtual_variable</p>
<p>i think it would be cool if this would be possible in Ruby code too</p> Ruby master - Feature #8366 (Open): Exception.message take time to execute depending on the insta...https://bugs.ruby-lang.org/issues/83662013-05-04T08:11:37Zanuraguniyal (anurag uniyal)anuraguniyal@gmail.com
<p>I am seeing very interesting and catastrophic behavior with ruby, see the code below</p>
<pre><code>class ExceptionTest
def test
@result = [0]*500000
begin
no_such_method
rescue Exception => ex
puts "before #{ex.class}"
st = Time.now
ex.message
puts "after #{Time.now-st} #{ex.message}"
end
end
end
ExceptionTest.new.test
</code></pre>
<p>Ideally ex.message should not take any time to execute and hence Time taken should be in ms, but here is the output</p>
<pre><code>ruby 1.9.2p290 (2011-07-09 revision 32553) [x86_64-darwin11.4.2]
before NameError
after 0.462443 undefined local variable or method `no_such_method' for #<ExceptionTest:0x007fc74a84e4f0>
</code></pre>
<p>If I assign <code>[0]*500000</code> to a local variable instead of instance variable e.g. <code>result = [0]*500000</code> it runs as expected</p>
<pre><code>ruby 1.9.2p290 (2011-07-09 revision 32553) [x86_64-darwin11.4.2]
before NameError
after 2.8e-05 undefined local variable or method `no_such_method' for #<ExceptionTest:0x007ff59204e518>
</code></pre>
<p><strong>It looks like somehow <code>ex.message</code> is looping thru the instance variables, why it would do so, please enlighten me!</strong></p>
<p>I have tried it on ruby ruby-1.9.2-p290, ruby-1.9.1-p376 and whatever version the ruby on codepad.org is.</p> Ruby master - Feature #8321 (Open): Ripper: I would like coordinates for keywordshttps://bugs.ruby-lang.org/issues/83212013-04-25T08:10:04Zericp (Eric Promislow)eric.promislow@gmail.com
<p>=begin<br>
Ripper gives the (({[line, column]})) coordinates for identifiers, strings, and numbers.</p>
<p>I would like it if it appended those coordinates to most of the block keywords,<br>
including (({:program})), (({:if})), (({:while})), (({:unless})), (({:end})), (({:def})), (({:class})), (({:module})), etc. As with the<br>
identifiers, it should go at the end. So an (({if}))-block would be represented as<br>
[0] :if<br>
[1] CONDITION<br>
[2] BLOCK<br>
[3] [:elsif, ...] || [:else, ...] || nil<br>
[4] [lineNo, colNo] # location of the leading :if/:elsif/:else/:unless</p>
<p>I currently get the first coordinate of ((%CONDITION%)), and then look up the preceding<br>
(({:if}))/(({:elsif}))/(({:else})) using (({Ripper.lex(src).find_all{|posn kwd name| kwd == :on_kw && %w/if else elsif/.include?(name) }}))</p>
<p>So the info is in Ripper. It would be more convenient if I could get that info in the src tree.</p>
<p>Note that my suggestion won't break (most) existing code, as the new data goes at the end<br>
of the list.</p>
<p>The same would be useful for other keywords, including (({:module})) (({:class})) (({:def})) (({:try})) (({:catch})) (({:begin})) (({:rescue})).<br>
=end</p> Ruby master - Feature #8184 (Open): Avoid the creation of meaningless ranges (nil, false, true)https://bugs.ruby-lang.org/issues/81842013-03-29T23:16:36Zphi (Pascal Hurni)hurnip@gmail.com
<p>With the definition of <=> on Object on post 1.8, the range_init() function no more throws "ArgumentError: bad value for range" when creating these ranges:<br>
nil..nil<br>
false..false<br>
true..true</p>
<p>These kind of ranges are meaningless, the majority of methods on Range do not work on them, like (nil..nil).include?(nil).</p>
<p>This feature request (with patch) re-introduces the 1.8 behaviour of throwing ArgumentError when creating such ranges.</p> Ruby master - Feature #7981 (Open): ruby does not respect --https://bugs.ruby-lang.org/issues/79812013-02-28T08:40:08Zmpapis (Michal Papis)mpapis@gmail.com
<p>=begin<br>
in ((%ruby --help%)) I have found:</p>
<pre><code>Usage: ruby [switches] [--] [programfile] [arguments]
</code></pre>
<p>so I have tried this example:</p>
<pre><code>$ echo 'puts :ok' | ruby -- --params
> ruby: No such file or directory -- --params (LoadError)
</code></pre>
<p>I have assumed that ((%[argumments]%)) are independent from ((%[programfile]%))</p>
<p>In the current state of code the help message should be:</p>
<pre><code>Usage: ruby [switches] [--] [programfile [arguments]]
</code></pre>
<p>But I would prefer that it would be fixed and anything after ((%[--]%)) that is not program gets ignored, so we can use the shell style:</p>
<pre><code>$ echo 'echo "args:$*:"' | bash -s -- --params
> args:--params:
</code></pre>
<p>=end</p>