Ruby Issue Tracking System: Issueshttps://bugs.ruby-lang.org/https://bugs.ruby-lang.org/favicon.ico?17113305112023-04-26T06:13:55ZRuby Issue Tracking System
Redmine Test - Feature #19618 (Open): hash#dig testshttps://bugs.ruby-lang.org/issues/196182023-04-26T06:13:55Zhdiwan415 (Hasan Diwan)
<p>diff --git a/spec/ruby/core/hash/hash_spec.rb b/spec/ruby/core/hash/hash_spec.rb<br>
index 2ccb483120..3b583b338b 100644<br>
--- a/spec/ruby/core/hash/hash_spec.rb<br>
+++ b/spec/ruby/core/hash/hash_spec.rb<br>
@@ -1,4 +1,4 @@<br>
-require_relative '../../spec_helper'<br>
+require_relative "../../spec_helper"</p>
<p>describe "Hash" do<br>
it "includes Enumerable" do<br>
@@ -6,9 +6,18 @@<br>
end<br>
end</p>
<p>+describe "Hash#dig" do</p>
<ul>
<li>it "handles nested hash" do</li>
<li>{ foo: { bar: { baz: 2 } } }.dig(:foo).should == { :bar => { :baz => 2 } }</li>
<li>{ foo: { bar: { baz: 2 } } }.dig(:foo, :bar).should == { :baz => 2 }</li>
<li>{ foo: { bar: { baz: 2 } } }.dig(:foo, :bar, :baz).should == 2</li>
<li>{ foo: { bar: { baz: 2 } } }.dig(:BAZ).should.nil?</li>
<li>end<br>
+end</li>
<li>
</ul>
<p>describe "Hash#hash" do<br>
it "returns a value which doesn't depend on the hash order" do</p>
<ul>
<li>{ 0=>2, 11=>1 }.hash.should == { 11=>1, 0=>2 }.hash</li>
</ul>
<ul>
<li>{ 0 => 2, 11 => 1 }.hash.should == { 11 => 1, 0 => 2 }.hash<br>
end</li>
</ul>
<p>it "returns a value in which element values do not cancel each other out" do<br>
@@ -26,9 +35,9 @@<br>
end</p>
<p>it "returns the same hash for recursive hashes" do</p>
<ul>
<li>h = {} ; h[:x] = h</li>
<li>h.hash.should == {x: h}.hash</li>
<li>h.hash.should == {x: {x: h}}.hash</li>
</ul>
<ul>
<li>h = {}; h[:x] = h</li>
<li>h.hash.should == { x: h }.hash</li>
<li>h.hash.should == { x: { x: h } }.hash
<a name="This-is-because-heqlx-h"></a>
<h1 >This is because h.eql?(x: h)<a href="#This-is-because-heqlx-h" class="wiki-anchor">¶</a></h1>
<a name="Remember-that-if-two-objects-are-eql"></a>
<h1 >Remember that if two objects are eql?<a href="#Remember-that-if-two-objects-are-eql" class="wiki-anchor">¶</a></h1>
<a name="then-the-need-to-have-the-same-hash"></a>
<h1 >then the need to have the same hash.<a href="#then-the-need-to-have-the-same-hash" class="wiki-anchor">¶</a></h1>
</li>
</ul>
<p>@@ -36,9 +45,9 @@<br>
end</p>
<p>it "returns the same hash for recursive hashes through arrays" do</p>
<ul>
<li>h = {} ; rec = [h] ; h[:x] = rec</li>
<li>h.hash.should == {x: rec}.hash</li>
<li>h.hash.should == {x: [h]}.hash</li>
</ul>
<ul>
<li>h = {}; rec = [h]; h[:x] = rec</li>
<li>h.hash.should == { x: rec }.hash</li>
<li>h.hash.should == { x: [h] }.hash
<a name="Like-above-because-heqlx-h"></a>
<h1 >Like above, because h.eql?(x: [h])<a href="#Like-above-because-heqlx-h" class="wiki-anchor">¶</a></h1>
</li>
</ul>
<p>end</p>
<p>@@ -47,7 +56,7 @@<br>
a = 1<br>
b = 2</p>
<ul>
<li>
<pre><code>eval('{a:, b:}.should == { a: 1, b: 2 }')
</code></pre>
</li>
</ul>
<ul>
<li>
<pre><code> eval("{a:, b:}.should == { a: 1, b: 2 }")
</code></pre>
end<br>
end<br>
end</li>
</ul>
<a name="adds-tests-forHashdig"></a>
<h1 >adds tests for.Hash#dig<a href="#adds-tests-forHashdig" class="wiki-anchor">¶</a></h1> Test - Bug #19186 (Open): test2https://bugs.ruby-lang.org/issues/191862022-12-06T01:25:49Zshugo (Shugo Maeda)
<p>Hello, this is a test issue.<br>
This is a second line with umlaut: ü</p> Test - Bug #19185 (Open): testhttps://bugs.ruby-lang.org/issues/191852022-12-06T01:24:24Zshugo (Shugo Maeda)
<p>hello, this is a test issue.<br>
This is a second line.</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 #11312 (Open): Add Resolv::DNS::Resource::IN::SPFhttps://bugs.ruby-lang.org/issues/113122015-06-26T18:58:49Zpostmodern (Hal Brodigan)postmodern.mod3@gmail.com
<p>The SPF type record (RFC4408 Section 3.1.1) is quite common now. The resolv library should probably support it. As a workaround I monkey patched in the resource class:</p>
<pre><code>class Resolv::DNS::Resource::IN::SPF < Resolv::DNS::Resource::IN::TXT
# resolv.rb doesn't define an SPF resource type.
TypeValue = 99
ClassValue = Resolv::DNS::Resource::IN::ClassValue
Resolv::DNS::Resource::ClassHash[[TypeValue, ClassValue]] = self
end
</code></pre>
<p>I could formalize my monkeypatch into an actual patch. I would just need to know whether SPF should be listed in the <code>ClassInsensitiveTypes</code> array.</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 #11307 (Open): exception-free non-blocking Queue#pophttps://bugs.ruby-lang.org/issues/113072015-06-25T23:06:21Znormalperson (Eric Wong)normalperson@yhbt.net
<p>As we learned from the nonblocking IO APIs, exceptions are noisy with debugging<br>
enabled and waste allocations on backtraces. We should have a better way to do<br>
non-blocking Queue#pop without raising exceptions, but I don't know what the API<br>
should be...</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 - Feature #11299 (Open): [PATCH] use Array instead of custom struct for generic ivarshttps://bugs.ruby-lang.org/issues/112992015-06-23T23:23:15Znormalperson (Eric Wong)normalperson@yhbt.net
<p>I'll commit this in a few days unless there's an objection.</p>
<pre><code>This reduces both code and object size while reducing the cognitive
overhead necessary to understand the code. Memory usage should be
slightly higher due to Array overheads, but still better than what we
had in Ruby 2.2 and earlier.
text data bss dec hex filename
2837117 22688 71576 2931381 2cbab5 ruby.before
2836221 22688 71576 2930485 2cb735 ruby.after
array.c | 28 +++++-----
internal.h | 1 +
variable.c | 170 +++++++++++++++++++------------------------------------------
3 files changed, 69 insertions(+), 130 deletions(-)
* array.c (rb_mem_clear): use memfill
(rb_ary_store_fill): new function
(rb_ary_store): use rb_ary_store_fill
* internal.h (rb_ary_store_fill): new declaration
* variable.c (struct gen_ivtbl): remove
(struct ivar_update): adjust type
(struct gen_ivar_compat_tbl): ditto
(struct gen_ivar_tag): ditto
(struct givar_copy): ditto
(gen_ivar_compat_tbl_i): adjust for type change
(gen_ivtbl_get): ditto
(generic_ivar_delete): ditto
(generic_ivar_get): ditto
(generic_ivar_update): ditto
(generic_ivar_defined): ditto
(rb_mark_generic_ivar): ditto
(rb_free_generic_ivar): ditto
(rb_generic_ivar_memsize): ditto
(generic_ivar_set): ditto
(gen_ivtbl_count): ditto
(gen_ivar_each_i): ditto
(gen_ivar_copy): ditto
(rb_copy_generic_ivar): ditto
(rb_ivar_count): ditto
(gen_ivtbl_bytes): remove
(gen_ivtbl_resize): remove
(gen_ivtbl_dup): remove
(gen_ivtbl_mark): remove
</code></pre> Ruby master - Feature #11292 (Open): objspace: Dump type of special constshttps://bugs.ruby-lang.org/issues/112922015-06-20T21:03:31Zsorah (Sorah Fukumori)her@sorah.jp
<p>related to <a class="issue tracker-1 status-5 priority-4 priority-default closed" title="Bug: [PATCH] Check SPECIAL_CONST_P before RBASIC_CLASS to avoid SEGV (Closed)" href="https://bugs.ruby-lang.org/issues/11291">#11291</a>, fix at r50982 doesn't put passed immediate value's type on output. This patch changes to include them.</p>
<p>I'm not sure this patch is suitable and correct, so opening feature request for review.</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 #11122 (Open): exception-free non-blocking Queue/SizedQueue operationshttps://bugs.ruby-lang.org/issues/111222015-05-06T20:53:09Znormalperson (Eric Wong)normalperson@yhbt.net
<p>I would like to reduce exceptions for non-blocking operations on<br>
Queue/SizedQueue in the same way we are reducing exceptions for non-blocking<br>
I/O with *_nonblock(..., exception: false) methods.</p>
<p>However, I'm unclear what the API would be, since queues return Ruby objects<br>
instead of String buffers or number of bytes written, so any special<br>
object/symbol we would return could conflict with existing applications.</p>
<p>Perhaps something like:</p>
<pre><code>queue.pop(nonblock: :WAIT)
queue.push(obj, nonblock: :WAIT)
</code></pre>
<p>Which would allow user to specify which object to raise when a queue is empty<br>
or full (similar to Timeout.timeout allowing specific exception to raise).</p> Ruby master - Feature #10932 (Open): Enabling allocation tracing as early as possiblehttps://bugs.ruby-lang.org/issues/109322015-03-03T19:53:30Ztenderlovemaking (Aaron Patterson)tenderlove@ruby-lang.org
<p>Hi,</p>
<p>I'd like to be able to enable allocation tracing as early as possible. I've attached a patch that I use to do that when I'm debugging code that I don't know very well.</p>
<p>The way I use it is like this:</p>
<p>$ ruby -robjspace/trace_start some_test.rb</p>
<p>I like using this because many times I can't figure out when to enable object tracing. Can we add this feature to object space?</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 - Misc #10628 (Open): Peformance of URI modulehttps://bugs.ruby-lang.org/issues/106282014-12-21T14:43:44Ztgxworld (Guo Xiang Tan)gxtan1990@gmail.com
<p>Please view attached screenshot or go to <a href="https://railsbench.herokuapp.com/tgxworld/ruby?utf8=%E2%9C%93&result_types%5B%5D=app_uri&commit=Submit" class="external">the following link</a> to see benchmark graph over time.</p>
<p>It got slower after this <a href="https://github.com/ruby/ruby/commit/bb83f32dc3e0424d25fa4e55d8ff32b061320e41" class="external">commit</a>.</p>
<p>Hope this helps.</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 #10574 (Open): Add String#lchomp and String.lchomp!https://bugs.ruby-lang.org/issues/105742014-12-05T20:37:41Zjavawizard (Alex Boyd)alex@opengroove.org
<p>With the expected behavior, i.e.</p>
<pre><code>irb(main):013:0> 'foobar'.lchomp('foo')
=> "bar"
irb(main):014:0> 'foobar'.lchomp('baz')
=> "foobar"
</code></pre>
<p>A quick google search will turn up plenty of practical uses for this, as well as lots of libraries that patch <code>String</code> themselves to add this.</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 - Misc #10513 (Open): instance_eval yields the receiver, but is documented to yield n...https://bugs.ruby-lang.org/issues/105132014-11-14T22:29:49Zctm (Cliff Matthews)ctm@devctm.com
<p>instance_eval yields the receiver, but is documented as yielding no arguments.</p>
<p>I searched the bug reports before writing this up and found bug <a class="issue tracker-1 status-8 priority-4 priority-default closed" title="Bug: instance_eval ArgumentError (Third Party's Issue)" href="https://bugs.ruby-lang.org/issues/2476">#2476</a> which was closed with a message that contained "instance_eval yields the receiver in both 1.8 and 1.9. Unfortunately, a Proc object created by lambda raises ArgumentError when extra arguments are yielded in 1.9.". However such behavior is not expected when the calling sequence is:</p>
<pre><code> * call-seq:
* obj.instance_eval(string [, filename [, lineno]] ) -> obj
* obj.instance_eval {| | block } -> obj
</code></pre>
<p>This discrepancy surprised me, but once I realized what was going on I simply used instance_exec instead of instance_eval. All else equal, I would prefer for instance_eval to to not yield the receiver since I can pick up the receiver as self if I want to, but such a change could breaking existing code, so probably documenting the current behavior is better.</p>
<pre><code>bash-3.2$ ruby --version
ruby 2.1.3p242 (2014-09-19 revision 47630) [x86_64-darwin14.0]
bash-3.2$ ./instance_eval
This is the lambda that *does* work.
arg = 32
self = 32
./instance_eval:6:in `block in <main>': wrong number of arguments (1 for 0) (ArgumentError)
from ./instance_eval:17:in `instance_eval'
from ./instance_eval:17:in `<main>'
</code></pre> 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 #10404 (Open): Allow individual finalizers to be removed with ObjectSpace.u...https://bugs.ruby-lang.org/issues/104042014-10-20T08:14:36Zjavawizard (Alex Boyd)alex@opengroove.org
<p>Pretty self-explanatory: allow removal of individual define_finalizer blocks without removing all of them as undefine_finalizer currently does.</p>
<p>From an API standpoint, this could be done by having define_finalizer return some sort of unique value (maybe an integer, or perhaps the type is unspecified), and this value could then be passed as undefine_finalizer's second argument to remove just that finalizer.</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 #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 - Bug #10009 (Open): IO operation is 10x slower in multi-thread environmenthttps://bugs.ruby-lang.org/issues/100092014-07-06T07:35:01Zariveira (Alexandre Riveira)alexandre@objectdata.com.br
<p>I created this issue <a class="issue tracker-5 status-1 priority-4 priority-default" title="Misc: better concurrency in threads (Open)" href="https://bugs.ruby-lang.org/issues/9832">#9832</a> but not have io operation.<br>
In the script attached I simulate IO operation in multi-thread environment.<br>
For ruby 1.9.2 apply <code>taskset -c -p 2 #{Process.pid}</code> for regulates threads behavior.<br>
The second Thread is a io operation</p>
<p>My results:</p>
<ol>
<li>
<p>ruby 2.1.2<br>
first 43500194<br>
second 95<br>
third 42184385</p>
</li>
<li>
<p>ruby-2.0.0-p451<br>
first 38418401<br>
second 95<br>
third 37444470</p>
</li>
<li>
<p>1.9.3-p545<br>
first 121260313<br>
second 50<br>
third 44275164</p>
</li>
<li>
<p>1.9.2-p320<br>
first 31189901<br>
second 897 <============<br>
third 31190598</p>
</li>
</ol>
<p>Regards</p>
<p>Alexandre Riveira</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 #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 #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 #9070 (Open): Introduce `---` as synonym of `end` keywordhttps://bugs.ruby-lang.org/issues/90702013-11-01T23:17:58Zalexeymuranov (Alexey Muranov)
<p>=begin<br>
This is just an idea: introduce "(({---}))" as synonym of "(({end}))" keyword.</p>
<p>It is a bit easier to type and to read, and makes whitespace insensitive language look as if it follows off-side rule. Compare:</p>
<h2>class Person<br>
attr_reader :name, :age<br>
def initialize(name, age)<br>
@name, @age = name, age<br>
---<br>
def <=>(person) # the comparison operator for sorting<br>
age <=> person.age<br>
---<br>
def to_s<br>
"#{name} (#{age})"<br>
---</h2>
<p>class Person<br>
attr_reader :name, :age<br>
def initialize(name, age)<br>
@name, @age = name, age<br>
end<br>
def <=>(person) # the comparison operator for sorting<br>
age <=> person.age<br>
end<br>
def to_s<br>
"#{name} (#{age})"<br>
end<br>
end</p>
<p>=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 #8663 (Open): Officialy alias ArgumentError to ArgErrorhttps://bugs.ruby-lang.org/issues/86632013-07-23T00:07:56ZAnonymous
<p>When not using custom made exceptions, I find myself using TypeError, NameError and ArgumentError the most. ArgumentError is the longest and I would like to suggest to alias it officially as ArgError.</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 #8478 (Open): The hash returned by Enumerable#group_by should have an empty...https://bugs.ruby-lang.org/issues/84782013-06-03T04:37:46Zphiggins (Pete Higgins)pete@peterhiggins.org
<p>Without this patch, nil checks might need to be done on the return value of Enumerable#group_by:</p>
<p>$ cat test_group_by.rb<br>
a = [1, 2, 3, "a", "b"]<br>
g = a.group_by {|o| o.class }</p>
<p>puts "Fixnums: #{g[Fixnum].size}"<br>
puts "Strings: #{g[String].size}"<br>
puts "Arrays: #{g[Array].size}"</p>
<p>$ ruby test_group_by.rb<br>
Fixnums: 3<br>
Strings: 2<br>
test_group_by.rb:6:in <code><main>': undefined method </code>size' for nil:NilClass (NoMethodError)</p>
<p>This patch adds a default value of an empty array to the hash returned by Enumerable#group_by, so the script above will work:</p>
<p>$ ./ruby -I.:lib test_group_by.rb<br>
Fixnums: 3<br>
Strings: 2<br>
Arrays: 0</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 #8449 (Open): Array#ary_plus always returns an arrayhttps://bugs.ruby-lang.org/issues/84492013-05-25T19:48:03Ztxemagon (José M. González)txema.gonz@gmail.com
<p>=begin</p>
<p>Even when Array is subclassified, + operator keeps on returning an Array.</p>
<p>Don't know if it is a bug or intentional, but it has surprised me for sure.</p>
<p>I paste some code for demonstrational purposes:</p>
<p>#!/usr/bin/env ruby<br>
#ary_plus.rb</p>
<a name="Shows-an-unexpected-behavior-to-me"></a>
<h1 >Shows an unexpected behavior to me<a href="#Shows-an-unexpected-behavior-to-me" class="wiki-anchor">¶</a></h1>
<a name="when-adding-Array-subclasess"></a>
<h1 >when adding Array subclasess.<a href="#when-adding-Array-subclasess" class="wiki-anchor">¶</a></h1>
<p>class Subarray < Array; end</p>
<p>a = Subarray.new<br>
b = Subarray.new</p>
<p>result = a + b</p>
<a name="I-was-expecting-to-have-a-Subarray-back"></a>
<h1 >I was expecting to have a Subarray back<a href="#I-was-expecting-to-have-a-Subarray-back" class="wiki-anchor">¶</a></h1>
<p>unless result.is_a? Subarray<br>
puts "result is not a [Subarray]"<br>
end</p>
<p>puts "result variable is a [#{result.class.name}]"</p>
<p>=end</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 #8232 (Open): Rudiments of abstract algebra in Rubyhttps://bugs.ruby-lang.org/issues/82322013-04-07T18:25:12ZAnonymous
<p>I have recently been struggling with Matrix class to make it accept physical magnitudes for matrix multiplication, and at that opportunity (<a href="http://bugs.ruby-lang.org/issues/8223" class="external">http://bugs.ruby-lang.org/issues/8223</a>), I noticed that rings and fields in Ruby do not know their additive identity. Eg. there is no method Float#zero or Rational#zero... I therefore propose that:</p>
<ol>
<li>every ring has #additive_identity, alias #zero method defined.</li>
<li>every ring has other methods defined, as required for rings in abstract algebra. An example (perhaps a stupid example) might be:</li>
</ol>
<p>class << Integer<br>
def additive_identity; 0 end<br>
alias zero additive_identity<br>
def add( other ); self + other end<br>
def additive_inverse; -self end<br>
def multiply( other ); self * other end<br>
def multiplicative_identity; 1 end<br>
end</p>
<ol start="3">
<li>That every field in Ruby has, in addition to the above methods, a method #multiplicative_inverse defined, as in:</li>
</ol>
<p>class << Float<br>
def additive_identity; 0.0 end<br>
alias zero additive_identity<br>
def add( other ); self + other end<br>
def additive_inverse; -self end<br>
def multiply( other ); self * other end<br>
def multiplicative_identity; 1.0 end<br>
alias one multiplicative_identity<br>
def multiplicative_inverse; 1.0 / self end<br>
end</p>
<p>I am no pro mathematician, and abstract algebra first sounded to me like a kind of thing that should be treated in some specialized libraries for math nerds, but looking how Twitter pays people to write abstract algebra in Scala</p>
<p><a href="https://github.com/scalaz/scalaz/blob/master/core/src/main/scala/scalaz/Monoid.scala" class="external">https://github.com/scalaz/scalaz/blob/master/core/src/main/scala/scalaz/Monoid.scala</a></p>
<p>and reading posts like this one about it:</p>
<p><a href="http://stackoverflow.com/questions/14790588/what-is-twitters-interest-in-abstract-algebra" class="external">http://stackoverflow.com/questions/14790588/what-is-twitters-interest-in-abstract-algebra</a></p>
<p>, where especially noteworthy comment is that by Randall Schulz of box.com, fourth from the top.</p>
<p>If we actually require Ruby rings and fields to have the basic properties of rings and fields (just like Enumerable classes are required to have #each method), it would be possible to implement structured objects such as Matrices over them, and instead of intuitively using numeric literals such as 0 and 1, the matrix or another structured object would ask rings / fields, which their elements come from, what their #additive_identity (#zero), #multiplicative_identity (#one) is. And at the same time, I would like to express my wish that Matrix be made a standard part of Ruby, that does not need to be loaded by require.</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>