Ruby Issue Tracking System: Issueshttps://redmine.ruby-lang.org/https://redmine.ruby-lang.org/favicon.ico?17113305112016-02-07T20:23:43ZRuby Issue Tracking System
Redmine Ruby master - Feature #12057 (Open): Allow methods with `yield` to be called without a blockhttps://redmine.ruby-lang.org/issues/120572016-02-07T20:23:43Zalexeymuranov (Alexey Muranov)
<p>Trying to figure out how <code>yield</code> works in Python, i had the following idea.</p>
<p>Allow a method with <code>yield</code> to be called without a block. When a method encounters <code>yield</code>, if no block is given, the method returns an <code>Enumerator</code> object.</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">def</span> <span class="nf">f</span>
<span class="k">yield</span> <span class="s1">'a'</span>
<span class="k">yield</span> <span class="s1">'b'</span>
<span class="k">end</span>
<span class="n">e</span> <span class="o">=</span> <span class="n">f</span>
<span class="nb">puts</span> <span class="n">e</span><span class="p">.</span><span class="nf">next</span> <span class="c1"># => a</span>
<span class="nb">puts</span> <span class="n">e</span><span class="p">.</span><span class="nf">next</span> <span class="c1"># => b</span>
</code></pre>
<p>It seems that this is what <code>yield</code> in Python does, but in Python a function with <code>yield</code> cannot take a block. Why not to have both?</p> Ruby master - Feature #10528 (Open): Allow line breaks instead of commas in arrays, hashes, argum...https://redmine.ruby-lang.org/issues/105282014-11-20T11:14:35Zalexeymuranov (Alexey Muranov)
<p>Since the seemingly reasonable proposal <a class="issue tracker-2 status-6 priority-4 priority-default closed" title="Feature: Allow hash members delimited by \n inside of {} (Rejected)" href="https://redmine.ruby-lang.org/issues/8956">#8956</a> has been rejected because it "diverged so far from the original", i will dare to propose another version here.</p>
<p>I suggest to automatically convert line breaks inside parentheses <code>(...)</code>, inside "hashy" braces <code>{...}</code>, inside brackets <code>[...]</code>, and inside block argument lists <code>|...|</code>, if they are not preceded with a comma and skipping empty space, into a comma, while all other line breaks that are not preceded with a semicolon into a semicolon (skipping empty lines). Unless, of course, the line break is preceded with <code>\</code> or followed by a dot <code>.</code>.</p>
<p>I would be possible to write then:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">todo</span> <span class="o">=</span> <span class="p">[</span> <span class="s1">'shopping'</span>
<span class="s1">'cinema'</span>
<span class="s1">'restaurant'</span> <span class="p">]</span>
<span class="n">rules</span> <span class="o">=</span> <span class="p">{</span> <span class="s1">'evil'</span> <span class="o">=></span> <span class="s1">'bad'</span>
<span class="s1">'kind'</span> <span class="o">=></span> <span class="s1">'good'</span> <span class="p">}</span>
</code></pre> Ruby master - Feature #9947 (Open): Make `Object#send` and `Object#method` privatehttps://redmine.ruby-lang.org/issues/99472014-06-16T14:19:20Zalexeymuranov (Alexey Muranov)
<p>I propose to make <code>Object#send</code> and <code>Object#method</code> private.</p>
<ul>
<li>If someone wants to use public <code>#send</code> or <code>#method</code> with their class, they can easily opt in :</li>
</ul>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">class</span> <span class="nc">MyClass</span>
<span class="kp">public</span> <span class="ss">:send</span><span class="p">,</span> <span class="ss">:method</span>
<span class="k">end</span>
</code></pre>
<ul>
<li>
<p>They can also use <code>BasicObject#__send__</code>. (There is however a mismatch between <code>BasicObject#__send__</code> and <code>Kernel#__method__</code>.)</p>
</li>
<li>
<p>There is hardly any good reason IMO to use public <code>#send</code> or <code>#method</code> with someone else's classes.</p>
</li>
<li>
<p>I am trying to never call <code>#send</code> or <code>#method</code> with an explicit receiver, but since <code>#send</code> and <code>#method</code> are public anyway, i feel that my code will be <em>under-appreciated</em> if someone looks at it.</p>
</li>
</ul> Ruby master - Feature #9941 (Open): Issue a warning when `module` or `class` keyword causes re-in...https://redmine.ruby-lang.org/issues/99412014-06-13T15:27:34Zalexeymuranov (Alexey Muranov)
<p>I think that it would be appropriate to issue a warning similar to "warning: already initialized constant" when "<code>module</code>" or "<code>class</code>" keyword causes re-initialization of a constant. Here is an example of code that looks suspicious to me and IMO should issue such a warning:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">module</span> <span class="nn">A</span>
<span class="k">module</span> <span class="nn">B</span>
<span class="no">C</span> <span class="o">=</span> <span class="mi">1</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="k">module</span> <span class="nn">D</span>
<span class="kp">include</span> <span class="no">A</span>
<span class="k">end</span>
<span class="nb">puts</span> <span class="no">D</span><span class="p">.</span><span class="nf">constants</span> <span class="c1"># => B</span>
<span class="nb">puts</span> <span class="no">D</span><span class="o">::</span><span class="no">B</span> <span class="c1"># => A::B</span>
<span class="nb">puts</span> <span class="no">D</span><span class="o">::</span><span class="no">B</span><span class="o">::</span><span class="no">C</span> <span class="c1"># => 1</span>
<span class="k">module</span> <span class="nn">D::B</span>
<span class="k">end</span>
<span class="nb">puts</span> <span class="no">D</span><span class="p">.</span><span class="nf">constants</span> <span class="c1"># => B</span>
<span class="nb">puts</span> <span class="no">D</span><span class="o">::</span><span class="no">B</span> <span class="c1"># => D::B</span>
<span class="nb">puts</span> <span class="no">D</span><span class="o">::</span><span class="no">B</span><span class="o">::</span><span class="no">C</span> <span class="c1"># => uninitialized constant D::B::C (NameError)</span>
</code></pre>
<p>P.S. I think this behavior is related to the impossibility to re-open anonymous classes/modules and to the somewhat strange IMO scoping rules for constants :).</p> Ruby master - Feature #9804 (Open): File::CREATE as a synonym for File::CREAThttps://redmine.ruby-lang.org/issues/98042014-05-05T16:53:35Zalexeymuranov (Alexey Muranov)
<p>I would like to have <code>File::CREATE</code> as a synonym for <code>File::CREAT</code>.</p> Ruby master - Feature #9678 (Feedback): New heredoc syntaxhttps://redmine.ruby-lang.org/issues/96782014-03-26T13:27:25Zalexeymuranov (Alexey Muranov)
<p>For whatever it is worth, i've just had this idea of a new heredoc syntax for some programming language:</p>
<pre><code>class C
def f(x)
print >>Message1 + >>Message2
Message1:
> Literal text
> ------------
> Here text.
>
Message2:
>> Text with interpolation and escapes
>> -----------------------------------
>> Here text with interpolation: #{ x }.
>>
end
end
</code></pre> Ruby master - Feature #9625 (Closed): Define `Pathname#/` to append to the pathhttps://redmine.ruby-lang.org/issues/96252014-03-12T09:12:47Zalexeymuranov (Alexey Muranov)
<p>How about borrowing this definition from <em>Homebrew</em> project:</p>
<pre><code>class Pathname
def / that
join that.to_s
end
end
</code></pre>
<p>Looks nice to me.</p> Ruby master - Feature #9405 (Rejected): Implicitly convert blocks to procs / remove blocks comple...https://redmine.ruby-lang.org/issues/94052014-01-13T11:58:55Zalexeymuranov (Alexey Muranov)
<p>While i am think of this, here is one more proposal.</p>
<p>If a block appears in a place where no block can appear, convert it automatically to a proc. For example:</p>
<pre><code>p = do |n; s| s = gets; n.times do puts s end end
t = {|| Time.now }
</code></pre>
<p>When a block takes no arguments and is written with curly braces, to distinguish it easily from a hash (or from a set, who knows? <a class="issue tracker-2 status-1 priority-4 priority-default" title="Feature: Add syntax to import Set into core (Open)" href="https://redmine.ruby-lang.org/issues/5478">#5478</a> :)), it would need to open with <code>{||</code>.</p>
<hr>
<p>Maybe ultimately blocks can be completely replaced by procs and lambdas from user's perspective and only existe for the compiler. Unfortunately, for compatibility, this would require treating specially literal procs in method calls (differently from procs and lambdas passed as variable or expression values. The semantics of</p>
<pre><code>foo(1) { |n| ... }
</code></pre>
<p>can be the following: the literal proc is converted to a block (which user never "sees") because it appears in a method call where no more arguments can appear.</p> Ruby master - Feature #9402 (Open): A syntax to specify the default value of a hashhttps://redmine.ruby-lang.org/issues/94022014-01-12T13:00:05Zalexeymuranov (Alexey Muranov)
<p>I propose the following syntax to specify the default value of a hash:</p>
<pre><code>h = { => 0, :a => 1, :b => 2 }
h[:a] # => 1
h[:c] # => 0
</code></pre> Ruby master - Feature #9401 (Open): Yet another syntax for literal anonymous functions (lambdas)https://redmine.ruby-lang.org/issues/94012014-01-12T10:44:54Zalexeymuranov (Alexey Muranov)
<p>Please do not be angry at me and just close this proposal if it does not look interesting. It comes from my aesthetic dissatisfaction with the <code>->(x){ ... }</code> literal anonymous function notation and from my amateurish interest in lambda calculus.</p>
<p>Here is a yet another syntax for literal anonymous functions (lambdas) that i propose:</p>
<pre><code>f = {\ x => x*x }
f[1] # => 1
f[2] # => 4
</code></pre>
<p>It looks a bit like a hash on purpose: i think that a hash is a "function in extension" and a lambda is a "function in intension" (see, for example, in <a href="http://www.classes.cs.uchicago.edu/archive/2004/spring/15300-1/docs/lambda-intro.pdf" class="external">these notes</a>). The backslash stands for "lambda", like in Haskell.</p> Ruby master - Feature #9095 (Open): Allow `Symbol#to_proc` to take argumentshttps://redmine.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://redmine.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://redmine.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 #8863 (Feedback): New error class: UndefinedBehaviorErrorhttps://redmine.ruby-lang.org/issues/88632013-09-05T04:30:40Zalexeymuranov (Alexey Muranov)
<p>=begin<br>
I propose to consider introducing a new error class: (({UndefinedBehaviorError})) (or (({UnspecifiedBehaviorError}))). It would be somewhat similar to (({NotImplementedError})), but it would mean that the behavior is actually unspecified, but may be defined in future versions (of Ruby or of the application).</p>
<p>The purpose is to raise it in "edge cases" of a newly introduced feature, to avoid users' relying on some unintended behavior, and to be able to change the behavior or to raise a different type of error in a future version.</p>
<p>I was thinking about a possible need for something like this in relation to (({Enumerable#to_h})) <a class="issue tracker-2 status-5 priority-4 priority-default closed" title="Feature: Enumerable#to_h (Closed)" href="https://redmine.ruby-lang.org/issues/7292">#7292</a>.<br>
=end</p> Ruby master - Feature #8361 (Closed): Alternative syntax for block parameterhttps://redmine.ruby-lang.org/issues/83612013-05-03T08:00:04Zalexeymuranov (Alexey Muranov)
<p>=begin<br>
I propose an alternative syntax for block parameters:</p>
<p>p = lambda [x] {<br>
# do whatever you like with x here, but be sure to return the result<br>
}</p>
<p>instead of</p>
<p>p = lambda { |x|<br>
# ...<br>
}</p>
<p>This would be consistent with the syntax of procedure call: (({p[a]})).</p>
<p>Also, compare:</p>
<p>a_bunch.each [element] do<br>
# ...<br>
end</p>
<p>with:</p>
<p>a_bunch.each do |element|<br>
# ...<br>
end</p>
<p>=end</p> Ruby master - Bug #8359 (Rejected): If p is a proc, p.call(x) can take a block, but p[x] cannothttps://redmine.ruby-lang.org/issues/83592013-05-02T19:46:59Zalexeymuranov (Alexey Muranov)
<p>=begin<br>
format = lambda { |l, s = '', &w|<br>
l.map {|e| w ? w[e] : e}.join(s)<br>
}</p>
<p>format.call([1, 2], "\n") {|c| "[ #{ c } ]"}</p>
<a name="gt-1-n-2-"></a>
<h1 >=> "[ 1 ]\n[ 2 ]"<a href="#gt-1-n-2-" class="wiki-anchor">¶</a></h1>
<p>format[[1, 2], "\n"] {|c| "[ #{ c } ]"}</p>
<a name="gt-SyntaxError-unexpected-arg-expecting-end-of-input"></a>
<h1 >=> SyntaxError: unexpected { arg, expecting end-of-input<a href="#gt-SyntaxError-unexpected-arg-expecting-end-of-input" class="wiki-anchor">¶</a></h1>
<p>=end</p> Ruby master - Bug #8346 (Rejected): If a module is included, its public instance methods behave l...https://redmine.ruby-lang.org/issues/83462013-04-29T21:53:27Zalexeymuranov (Alexey Muranov)
<p>=begin<br>
This behavior is unexpected to me:</p>
<p>module M<br>
def f<br>
1<br>
end<br>
end<br>
include M</p>
<p>M.f # => 1</p>
<p>After including the module (({M})), the method (({f})) becomes an instance method of the module for no apparent reason. I would expect an error (instance method not found).<br>
=end</p> Ruby master - Bug #8189 (Closed): (one more) inconsistency between Class and Module as namespaceshttps://redmine.ruby-lang.org/issues/81892013-03-30T18:41:15Zalexeymuranov (Alexey Muranov)
<p>=begin<br>
(I will only mention in parentheses that inheriting (({Class})) from (({Module})) looks like a bad idea to me, i would have preferred that both inherited from some (({Namespace})) class.)</p>
<p>While trying to answer ((<this SO question|URL:<a href="http://stackoverflow.com/questions/6254496/is-it-possible-to-give-a-sub-module-the-same-name-as-a-top-level-class/15716986#15716986%3E" class="external">http://stackoverflow.com/questions/6254496/is-it-possible-to-give-a-sub-module-the-same-name-as-a-top-level-class/15716986#15716986></a>)), i have noticed the following inconsistent behavior between classes and modules used as namespaces:</p>
<p>A = 0</p>
<p>class C; end<br>
module M; end</p>
<p>C::A # warning: toplevel constant A referenced by C::A<br>
M::A # uninitialized constant M::A (NameError)<br>
=end</p> Ruby master - Bug #8136 (Closed): gsub strange behavior when substituting "\\'"https://redmine.ruby-lang.org/issues/81362013-03-21T19:05:08Zalexeymuranov (Alexey Muranov)
<p>=begin<br>
s = "\'" # => "\'"<br>
print s # '<br>
"foo".gsub("f", s) # => "oooo"</p>
<p>Can anybody please explain to me why it works like this? Is this a bug?<br>
=end</p> Ruby master - Feature #8038 (Rejected): Keyword `object` to be used instead of `class <<`https://redmine.ruby-lang.org/issues/80382013-03-07T14:59:08Zalexeymuranov (Alexey Muranov)
<p>=begin<br>
I propose to introduce a new keyword (({object})) and to use it instead of the mysterious (({class <<})):</p>
<p>object foo<br>
def bar<br>
# ...<br>
end<br>
def baz<br>
# ...<br>
end<br>
end</p>
<p>instead of</p>
<p>foo = Object.new<br>
class << foo<br>
def bar<br>
# ...<br>
end<br>
def baz<br>
# ...<br>
end<br>
end<br>
=end</p> Ruby master - Feature #7994 (Rejected): Make iterators pass an implicit named parameter `iteratio...https://redmine.ruby-lang.org/issues/79942013-03-02T00:21:36Zalexeymuranov (Alexey Muranov)
<p>=begin<br>
I think it would be nice to be able to write something like this:</p>
<p>items.each do |item|<br>
unless iteration.first?<br>
# do something that is not applicable to the first iteration<br>
end<br>
# do whatever is to be done to all items<br>
end<br>
=end</p> Ruby master - Feature #7978 (Rejected): boolean to_ihttps://redmine.ruby-lang.org/issues/79782013-02-27T23:00:22Zalexeymuranov (Alexey Muranov)
<p>=begin<br>
The current behavior is the following:</p>
<blockquote>
<p>nil.to_i<br>
=> 0<br>
false.to_i<br>
NoMethodError: undefined method `to_i' for false:FalseClass</p>
</blockquote>
<blockquote>
<p>true.to_i<br>
NoMethodError: undefined method `to_i' for true:TrueClass</p>
</blockquote>
<p>This does not look very consistent to me. I think it could be useful to define (({false.to_i})) as 0 and (({true.to_i})) as 1. I think those are fairly common numeric values for False and True. These values as strings "0" and "1" are also commonly used in HTML forms to represent boolean values.<br>
=end</p> Ruby master - Feature #7872 (Rejected): `block_given?` does not work inside `define_method`https://redmine.ruby-lang.org/issues/78722013-02-17T10:40:09Zalexeymuranov (Alexey Muranov)
<p>=begin<br>
Is this the expected behavior?</p>
<p>define_method :try do<br>
block_given? ? yield : 'no block'<br>
end</p>
<p>try { 'block' } # => "no block"</p>
<p>However:</p>
<p>def try_again<br>
block_given? ? yield : 'no block'<br>
end</p>
<p>try_again { 'block' } # => "block"</p>
<p>=end</p> Ruby master - Feature #7796 (Feedback): Hash#keys should return a sethttps://redmine.ruby-lang.org/issues/77962013-02-07T16:52:07Zalexeymuranov (Alexey Muranov)
<p>=begin<br>
In my opinion, (({Hash#keys})) should return an instance of (({Set})).<br>
=end</p> Ruby master - Feature #7739 (Assigned): Define Hash#| as Hash#reverse_merge in Railshttps://redmine.ruby-lang.org/issues/77392013-01-24T21:57:20Zalexeymuranov (Alexey Muranov)
<p>=begin<br>
I suggest for to define (({Hash#|})) as (({Hash#reverse_merge})) in ((<em>Rails</em>)), in my opinion this would correspond nicely to (({Set#|})), to the logical (({#||})) and to the bitwise (({#|})):</p>
<p>{ :a => 1, :b => 2 } | { :b => 1, :c => 2 } # => { :a => 1, :b => 1, :c => 2 }<br>
=end</p> Ruby master - Feature #7738 (Rejected): Deprecate Set#+ as an alias of Set#|, use it for symmetri...https://redmine.ruby-lang.org/issues/77382013-01-24T20:36:38Zalexeymuranov (Alexey Muranov)
<p>=begin<br>
I am almost sure this will be rejected, but i want to try anyway. My goal is to propose a more efficient and consistent use of binary operator symbols for some classes, in this case for (({Set})) and (({Hash})).</p>
<p>I propose to deprecate (({Set#+})) as an alias of (({Set#|})), and use (({Set#+})) later for the ((<em>symmetric difference</em>)) of sets. I think that operator symbols like (({+})) and (({|})) are too precious to alias one another.</p>
<p>Currently (({Set#+})) is probably the only use of (({#+})) for an operation which is not ((<em>injective</em>)) in each of the arguments: for sets (({a})), (({b})), (({c})), the equality</p>
<p>a + b == a + c</p>
<p>does not currently imply</p>
<p>b == c</p>
<p>The natural binary operation on sets that is injective in each argument is the ((<em>symmetric difference</em>)), it corresponds to the bitwise XOR. I have also noticed that the "(({+}))" for sets is used in "Lectures on ergodic theory " by P. Halmos to denote the symmetric difference.</p>
<p>I also suggest for to define (({Hash#|})) as Hash#reverse_merge in Rails, in my opinion this would correspond nicely to (({Set#|})).<br>
=end</p> Ruby master - Feature #7546 (Open): Change behavior of `Array#slice` for an argument of `Range` c...https://redmine.ruby-lang.org/issues/75462012-12-12T01:23:22Zalexeymuranov (Alexey Muranov)
<p>=begin<br>
This is a concrete proposal to "fix" <a class="issue tracker-2 status-6 priority-4 priority-default closed" title="Feature: Inconsistent Array.slice() (Rejected)" href="https://redmine.ruby-lang.org/issues/4541">#4541</a>.</p>
<p>It is also related to <a class="issue tracker-2 status-1 priority-4 priority-default" title="Feature: Make Range act as a "lazy ordered set" (Open)" href="https://redmine.ruby-lang.org/issues/7545">#7545</a>.<br>
For this proposal to make good sense, i think it would be nice if <a class="issue tracker-2 status-1 priority-4 priority-default" title="Feature: Make Range act as a "lazy ordered set" (Open)" href="https://redmine.ruby-lang.org/issues/7545">#7545</a> was at least partially accepted.</p>
<p>=== Main proposal</p>
<p>I propose (({Array#slice})) with (({Range})) type argument to work as follows:</p>
<p>a = ['0', '1', '2', '3']<br>
a[1..2] # => ['1', '2']<br>
a[-2..-1] # => ['2', '3']<br>
a[2..1] # => ['2', '1']<br>
a[-1..-2] # => ['3', '2']<br>
a[-1..1] # => ['3', '0', '1']<br>
a[1..-1] # => ['1', '0', '3']<br>
a[1..1] # => ['1']<br>
a[1...1] # => []<br>
a[4..4] # => [nil]<br>
a[4...4] # => []<br>
a[9..9] # => [nil]<br>
a[9...9] # => []<br>
a[1..5] # => ['1', '2', '3', nil, nil]</p>
<p>=== Secondary proposal: consider adding new instance methods to (({Array})) to compensate the changed behavior of (({Array#slice}))</p>
<p>If this proposal is accepted, the code "(({a[1..-2]}))" for an array (({a})) will not work as before.<br>
This can be compensated by adding new instance methods to (({Array})).<br>
For example the following ones.</p>
<ol>
<li>(({Array#clip(fixnum, fixnum)})):</li>
</ol>
<p>['0', '1', '2', '3'].clip(1, 1) # => ['1', '2']</p>
<p>Thus (({a.clip(1, 1)})) would be a replacement for (({a[1..-2]})).</p>
<p>(It looks strange to have to convert a pair of numbers ((<em>m</em>)) and ((<em>n</em>)) into a range (({m..(-1-n)})) to simply ask an array to remove ((<em>m</em>)) elements from the beginning and ((<em>n</em>)) elements from the end.<br>
If <a class="issue tracker-2 status-1 priority-4 priority-default" title="Feature: Make Range act as a "lazy ordered set" (Open)" href="https://redmine.ruby-lang.org/issues/7545">#7545</a> is accepted, then the "(({a[1..-2]}))" syntax for "clipping" an array will make not much sense and maybe will not be possible.)</p>
<ol start="2">
<li>(({Array#from(fixnum)})), (({Array#till(fixnum)})):</li>
</ol>
<p>a = ['0', '1', '2', '3']<br>
a.from(1) # => ['1', '2', '3']<br>
a.till(1) # => ['0', '1']<br>
a.from(1).till(-2) # => ['1', '2']</p>
<p>In fact, in ((<em>Rails</em>)) (({ActiveSupport})) there are methods (({Array#from})) and (({Array#to})) like this, but unfortunately they do not accept negative indices.</p>
<p>((<em>Remark</em>)). It would also be possible to have (({Array#clip!})), (({Array#from!})), (({Array#till!})).<br>
=end</p> Ruby master - Feature #7545 (Open): Make Range act as a "lazy ordered set"https://redmine.ruby-lang.org/issues/75452012-12-12T01:05:47Zalexeymuranov (Alexey Muranov)
<p>=begin</p>
<a name="Make-Range-act-as-a-lazy-ordered-set"></a>
<h1 >Make Range act as a "lazy ordered set"<a href="#Make-Range-act-as-a-lazy-ordered-set" class="wiki-anchor">¶</a></h1>
<p>This replaces my older feature request <a class="issue tracker-2 status-6 priority-4 priority-default closed" title="Feature: Redefine Range class and introduce RelativeNumeric and RelativeRange (Rejected)" href="https://redmine.ruby-lang.org/issues/5534">#5534</a>.</p>
<p>I propose the following new behavior of (({Range})):</p>
<p>(1..3).to_a # => [1, 2, 3]<br>
(3..1).to_a # => [3, 2, 1]<br>
'a'..'c'.to_a # => ['a', 'b', 'c']<br>
'c'..'a'.to_a # => ['c', 'b', 'a']<br>
1...1 == 3...3 # => true<br>
1...1 == 'a'...'a' # => true<br>
1...1 == Range::EMPTY_SET # => true<br>
1..2 == 1...3 # => false<br>
1...3.include?(2.5) # => true</p>
<p>Also, maybe in the future the following behavior can be possible:</p>
<p>r1 = Range.new('a', 'bc', :deglex)<br>
r1.include?('abc') # => false<br>
r1.to_a # => ['a', 'b', ..., 'az', 'ba', 'bb', 'bc']<br>
r2 = Range.new('a', 'bc', :lex)<br>
r2.include?('abc') # => true<br>
r2.to_a # => Error</p>
<p>and this:</p>
<p>((0.5)..(5.5)).to_a(Integer) # => [1, 2, 3, 4, 5]<br>
((0.5)..(5.5)).each(Integer) do |i| puts i end</p>
<p>(i imagine this would require additions to the (({Integer})) class, so that it<br>
know more about "real" ranges).</p>
<p>=end</p> Ruby master - Bug #7331 (Rejected): Set the precedence of unary `-` equal to the precedence `-`, ...https://redmine.ruby-lang.org/issues/73312012-11-12T02:20:19Zalexeymuranov (Alexey Muranov)
<p>=begin<br>
I will not be surprised if this proposal is rejected, because the compatibility would be a mess, but i feel i need to start a discussion. This continues the discussion in <a class="issue tracker-2 status-6 priority-4 priority-default closed" title="Feature: Move ** operator precedence under unary + and - (Rejected)" href="https://redmine.ruby-lang.org/issues/7328">#7328</a>.</p>
<p>To the best of my knowledge, in mathematics the unary minus has the same precedence as the binary one. However, in the expression</p>
<ul>
<li>a * b</li>
</ul>
<p>Ruby computes first (({-a})), and then the product. This means that if i want (for whatever reason) to compute the expression in the natural order, i have to put the parentheses:</p>
<ul>
<li>(a * b)</li>
</ul>
<p>which looks very strange to me. I would consider this a bug.<br>
=end</p> Ruby master - Feature #7322 (Rejected): Add a new operator name #>< for bit-wise "exclusive or"https://redmine.ruby-lang.org/issues/73222012-11-10T20:26:21Zalexeymuranov (Alexey Muranov)
<p>=begin<br>
I propose to alias (({Fixnum#^})) and (({Bignum#^})), which stand for bit-wise "exclusive or", with a new operator name (({#><})).</p>
<p>Is it necessary to go along with C, Python, and Mathematica?<br>
According to Wikipedia ( <a href="http://en.wikipedia.org/wiki/Exclusive_or#Alternative_symbols" class="external">http://en.wikipedia.org/wiki/Exclusive_or#Alternative_symbols</a> ), the "^" symbol is not used for XOR outside of programming context.</p>
<p>Here are some examples of mathematical notation: <a href="http://mathworld.wolfram.com/XOR.html" class="external">http://mathworld.wolfram.com/XOR.html</a> (there are also examples in the Wikipedia article).</p>
<p>The Unicode symbol is "⊻".</p>
<p>If eventually (({Fixnum#^})) as XOR is deprecated, this will liberate this symbol for other uses, for example for all kinds of exponential notations (for me, "a^n" usually means "the n-th power of a").<br>
=end</p> Ruby master - Feature #7250 (Open): A mechanism to include at once both instance-level and class-...https://redmine.ruby-lang.org/issues/72502012-10-31T18:59:56Zalexeymuranov (Alexey Muranov)
<p>=begin<br>
I have simply commented first on <a class="issue tracker-2 status-7 priority-4 priority-default closed" title="Feature: Inheritable #included/#extended Hooks For Modules (Feedback)" href="https://redmine.ruby-lang.org/issues/7240">#7240</a>, but i feel i need to start a ticket in order to not post off topic.</p>
<p>This seems to be a big feature request, so i didn't feel confident to suggest, but when i read in <a class="issue tracker-2 status-7 priority-4 priority-default closed" title="Feature: Inheritable #included/#extended Hooks For Modules (Feedback)" href="https://redmine.ruby-lang.org/issues/7240">#7240</a> that (({Module#included})) hook can become even more hooked, i've decided to express myself.</p>
<p>=== Proposal</p>
<p>In my opinion, the most common use case for the "(({included}))" hook in modules is adding class methods. However, to me this looks hackish and hard to follow. I would have preferred if there was a way to define both instance-level and class-level behavior in a module and include both at once. Here is an API i would suggest:</p>
<p>module M<br>
def foo<br>
'Foo'<br>
end</p>
<pre><code>def base.bar # here a fictional private method Module#base is called
'Bar'
end
</code></pre>
<p>end</p>
<p>class C<br>
include M<br>
end</p>
<p>a = C.new<br>
a.foo # => 'Foo'<br>
C.bar # => 'Bar'</p>
<p>This means that a module would need to store 2 method tables, one as usual, and a second one defined as singleton methods on some object returned by some private method, called for example (({Module#base})). Since ordinary objects have no method table, classes have one method table, why not to allow modules to have 2 method tables?</p>
<p>=== Relevant considerations</p>
<ol>
<li>
<p>Talking about method tables, maybe if objects were allowed to have one, there would be no need for meta-classes? (Then classes would have 2 method tables, modules would have 3, and in fact methods and method tables could be regarded as kinds of attributes.) It looks to me like meta-class is simply there to make up for the fact that objects are not allowed to keep their own methods.</p>
</li>
<li>
<p>Allowing modules to have more method tables than classes may look like breaking the inheritance (({Class < Module})), but to me this inheritance does not look like a good idea anyway. For example, a module can be included, but a class cannot. To me, class looks like an object factory, but a module like a container of parts that can be unpacked and mounted onto a class.</p>
</li>
</ol>
<p>=== Irrelevant considerations</p>
<p>I think that from the point of view of English, the method name "(({included}))" is not consistent with the method name "(({extended}))" (the <em>module</em> is included, but the <em>base</em> is extended).</p>
<p>I would have preferred if the word "(({extend}))" was used instead of "(({include}))" when "including" one module into another, as this operation seems somewhat different from including a module into a class. I understand that it makes no sense to discuss this now, when (({extend})) is already has its fixed meaning.<br>
=end</p> Ruby master - Feature #7114 (Feedback): New classes: `HumanTime::LocalTime`, `HumanTime::Duration`https://redmine.ruby-lang.org/issues/71142012-10-08T00:47:57Zalexeymuranov (Alexey Muranov)
<p>=begin<br>
As suggested by <em>drbrain</em> in <a class="issue tracker-2 status-6 priority-4 priority-default closed" title="Feature: New classes: `HumanTime::LocalTime`, `HumanTime::LocalDate`, `HumanTime::LocalDateTime` to repres... (Rejected)" href="https://redmine.ruby-lang.org/issues/7113">#7113</a> , here is a separate request for classes to hold the time of day and duration of time.</p>
<p>Ruby currently does not have a class to represent a standard database "time" column. Using (({Time})) for this causes some difficulties for example to users of <em>Ruby on Rails</em>: <a href="http://www.rebeccablyth.co.uk/2008/01/03/time-columns-and-rails/" class="external">http://www.rebeccablyth.co.uk/2008/01/03/time-columns-and-rails/</a><br>
=end</p> Ruby master - Feature #7113 (Rejected): New classes: `HumanTime::LocalTime`, `HumanTime::LocalDat...https://redmine.ruby-lang.org/issues/71132012-10-07T00:05:19Zalexeymuranov (Alexey Muranov)
<p>=begin<br>
I think it would be nice if the following new classes were added to the standard library: (({HumanTime::LocalTime})), (({HumanTime::LocalDate})), (({HumanTime::LocalDateTime})) to represent what they name. Apart from the time-related instance variables (({@year})), (({@month})), (({@day})), (({@weekday})), (({@hour})), (({@minutes})), (({@seconds})), (({@fraction_of_a_second})), they would have an optional string instance variable (({@location})), and maybe (({@longitude})) and (({@latitude})).</p>
<p>I think that the (({Time})) class is good for timestamping system log entires or stages in a physical experiment, but is not appropriate for "timetable time", like train schedule.</p>
<p>Here is an example: if the Mood falls on Earth and the Earth starts rotating faster, the days would become shorter, and it is clear that trains would not continue to follow their previous schedule described with Ruby (({Time})) instances, but they will try to follow the schedule described with "hh:mm" format relative to the new day length, which will stay 24 ((<em>human hours</em>)).</p>
<p>Another practical consideration: Ruby currently does not have a class to represent a standard database "time" column. This causes some unpleasant difficulties to users of ((<em>Ruby on Rails</em>)) web application framework, for example: <a href="http://www.rebeccablyth.co.uk/2008/01/03/time-columns-and-rails/" class="external">http://www.rebeccablyth.co.uk/2008/01/03/time-columns-and-rails/</a></p>
<p>What do you think?<br>
=end</p> Ruby master - Feature #7055 (Rejected): Allow `.foo=` as a synonym of `self.foo=`https://redmine.ruby-lang.org/issues/70552012-09-23T17:25:26Zalexeymuranov (Alexey Muranov)
<p>=begin<br>
I propose to make the use of <code>self</code> in method calls optional:</p>
<p>.foo = 'bar'</p>
<p>instead of</p>
<p>self.foo = 'bar'</p>
<p>This only matters of course for assignment methods, to distinguish a method call from creating or setting a local variable.<br>
=end</p> Ruby master - Feature #7019 (Rejected): allow `private` and `protected` keywords to take blockshttps://redmine.ruby-lang.org/issues/70192012-09-13T16:02:04Zalexeymuranov (Alexey Muranov)
<p>I like to indent my private methods one level deeper, but this indentation is inconsistent with the syntax:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">class</span> <span class="nc">C</span>
<span class="k">def</span> <span class="nf">f</span>
<span class="c1">#</span>
<span class="k">end</span>
<span class="kp">private</span>
<span class="k">def</span> <span class="nf">g</span>
<span class="c1">#</span>
<span class="k">end</span>
<span class="k">end</span>
</code></pre>
<p>I think it would be nice if the <code>private</code> keyword could take a block, then i would write:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">class</span> <span class="nc">C</span>
<span class="k">def</span> <span class="nf">f</span>
<span class="c1">#</span>
<span class="k">end</span>
<span class="kp">private</span> <span class="k">do</span>
<span class="k">def</span> <span class="nf">g</span>
<span class="c1">#</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="k">end</span>
</code></pre> Ruby master - Feature #6869 (Open): Do not treat `_` parameter exceptionallyhttps://redmine.ruby-lang.org/issues/68692012-08-15T06:50:54Zalexeymuranov (Alexey Muranov)
<p>I started by commenting on <a class="issue tracker-2 status-5 priority-4 priority-default closed" title="Feature: Don't warn for unused variables starting with _ (Closed)" href="https://redmine.ruby-lang.org/issues/6693">#6693</a>, but i have realized that this is a slightly different request.</p>
<p>I propose to not treat the variable name "<code>_</code>" exceptionally. Current behavior:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"> <span class="p">{</span><span class="mi">0</span><span class="o">=></span><span class="mi">1</span><span class="p">}.</span><span class="nf">each_with_index</span> <span class="p">{</span> <span class="o">|</span><span class="n">_</span><span class="p">,</span><span class="n">_</span><span class="o">|</span> <span class="nb">p</span> <span class="n">_</span> <span class="p">}</span> <span class="c1"># [0, 1]</span>
</code></pre>
<p>prints "[0, 1]", but</p>
<pre><code class="ruby syntaxhl" data-language="ruby"> <span class="p">{</span><span class="mi">1</span><span class="o">=></span><span class="mi">2</span><span class="p">}.</span><span class="nf">each_with_index</span> <span class="p">{</span> <span class="o">|</span><span class="n">x</span><span class="p">,</span><span class="n">x</span><span class="o">|</span> <span class="nb">p</span> <span class="n">x</span> <span class="p">}</span> <span class="c1"># SyntaxError: (eval):2: duplicated argument name</span>
</code></pre>
<p>raises "SyntaxError: (eval):2: duplicated argument name".</p>
<p>Similarly for methods:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"> <span class="k">def</span> <span class="nf">f</span><span class="p">(</span><span class="n">_</span><span class="p">,</span> <span class="n">_</span><span class="p">)</span>
<span class="n">_</span>
<span class="k">end</span>
<span class="n">f</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span> <span class="c1"># => 0</span>
<span class="k">def</span> <span class="nf">f</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">x</span><span class="p">)</span>
<span class="n">x</span>
<span class="k">end</span> <span class="c1"># => SyntaxError: (eval):2: duplicated argument name</span>
</code></pre>
<p>Observe also that the use of repeated <code>_</code> parameter is not consistent between methods and blocks: for methods the value is the first assigned value, and for blocks it is the array of all the assigned values.</p>
<ol>
<li>I propose to use the same rule for all variables, without distinguishing <code>_</code> specially.</li>
</ol>
<p>In particular i propose to allow to repeat any variable, not only <code>_</code>, in block or method arguments without raising an error.</p>
<p>There may be several solutions what the repeated argument will hold: it may hold the array of all assigned values, the first assigned value, the last assigned value, the first non-nil assigned value, or the last non-nil assigned value.</p>
<ol start="2">
<li>
<p>I propose to treat repeated arguments in methods and in blocks the same way (do not know which one).</p>
</li>
<li>
<p>For unused variables i propose to introduce a special placeholder, for example "<code>-</code>" not followed by anything other than a delimiter (comma or bracket):</p>
</li>
</ol>
<pre><code class="ruby syntaxhl" data-language="ruby"> <span class="n">each_with_index</span> <span class="p">{</span> <span class="o">|-</span><span class="p">,</span> <span class="n">value</span><span class="o">|</span> <span class="nb">puts</span> <span class="n">value</span> <span class="p">}</span>
<span class="o">-</span><span class="p">,</span> <span class="o">-</span><span class="p">,</span> <span class="n">suffix</span> <span class="o">=</span> <span class="n">parse</span><span class="p">(</span><span class="nb">name</span><span class="p">)</span>
</code></pre> Ruby master - Feature #6868 (Rejected): Make `do` in block syntax optional when the block is the ...https://redmine.ruby-lang.org/issues/68682012-08-15T01:26:24Zalexeymuranov (Alexey Muranov)
<p>=begin<br>
I propose to make the use of "(({do}))" in block syntax optional if the block is the last argument of a method and if it is a required argument, at least if the block does not take parameters. (I think this would be in line with how for example the last hash argument is treated.) I also think that this syntactic change may allow in future versions of Ruby to make (({def})), (({class})), (({module})) methods instead of keywords.</p>
<p>Something like:</p>
<p>3.time<br>
puts "Hi!"<br>
end</p>
<p>instead of</p>
<p>3.time do<br>
puts "Hi!"<br>
end</p>
<p>I know this is not a good example, because for (({#times})) the block argument is optional.<br>
=end</p> Ruby master - Feature #6810 (Assigned): `module A::B; end` is not equivalent to `module A; module...https://redmine.ruby-lang.org/issues/68102012-07-29T03:55:09Zalexeymuranov (Alexey Muranov)
<p>Is this the expected behavior? To me, it is rather surprising:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="no">N</span> <span class="o">=</span> <span class="mi">0</span>
<span class="k">module</span> <span class="nn">A</span>
<span class="k">module</span> <span class="nn">B</span>
<span class="k">def</span> <span class="nc">self</span><span class="o">.</span><span class="nf">f</span><span class="p">;</span> <span class="no">N</span><span class="p">;</span> <span class="k">end</span>
<span class="k">end</span>
<span class="no">N</span> <span class="o">=</span> <span class="mi">1</span>
<span class="k">end</span>
<span class="no">A</span><span class="o">::</span><span class="no">B</span><span class="p">.</span><span class="nf">f</span> <span class="c1"># => 1</span>
<span class="no">N</span> <span class="o">=</span> <span class="mi">0</span>
<span class="no">A</span><span class="o">::</span><span class="no">B</span><span class="p">.</span><span class="nf">f</span> <span class="c1"># => 0</span>
<span class="no">A</span><span class="o">::</span><span class="no">N</span> <span class="o">=</span> <span class="mi">1</span>
<span class="no">A</span><span class="o">::</span><span class="no">B</span><span class="p">.</span><span class="nf">f</span> <span class="c1"># => 1</span>
<span class="no">A</span><span class="o">::</span><span class="no">B</span><span class="o">::</span><span class="no">N</span> <span class="o">=</span> <span class="mi">2</span>
<span class="no">A</span><span class="o">::</span><span class="no">B</span><span class="p">.</span><span class="nf">f</span> <span class="c1"># => 2</span>
</code></pre>
<p>but</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="no">N</span> <span class="o">=</span> <span class="mi">0</span>
<span class="k">module</span> <span class="nn">A</span><span class="p">;</span> <span class="k">end</span>
<span class="k">module</span> <span class="nn">A::B</span>
<span class="k">def</span> <span class="nc">self</span><span class="o">.</span><span class="nf">f</span><span class="p">;</span> <span class="no">N</span><span class="p">;</span> <span class="k">end</span>
<span class="k">end</span>
<span class="k">module</span> <span class="nn">A</span>
<span class="no">N</span> <span class="o">=</span> <span class="mi">1</span>
<span class="k">end</span>
<span class="no">A</span><span class="o">::</span><span class="no">B</span><span class="p">.</span><span class="nf">f</span> <span class="c1"># => 0</span>
<span class="no">N</span> <span class="o">=</span> <span class="mi">0</span>
<span class="no">A</span><span class="o">::</span><span class="no">B</span><span class="p">.</span><span class="nf">f</span> <span class="c1"># => 0</span>
<span class="no">A</span><span class="o">::</span><span class="no">N</span> <span class="o">=</span> <span class="mi">1</span>
<span class="no">A</span><span class="o">::</span><span class="no">B</span><span class="p">.</span><span class="nf">f</span> <span class="c1"># => 0</span>
<span class="no">A</span><span class="o">::</span><span class="no">B</span><span class="o">::</span><span class="no">N</span> <span class="o">=</span> <span class="mi">2</span>
<span class="no">A</span><span class="o">::</span><span class="no">B</span><span class="p">.</span><span class="nf">f</span> <span class="c1"># => 2</span>
</code></pre> Ruby master - Feature #6806 (Feedback): Support functional programming: forbid instance/class var...https://redmine.ruby-lang.org/issues/68062012-07-28T22:26:42Zalexeymuranov (Alexey Muranov)
<p>What would you say about this proposal? Is there a better alternative?</p>
<p>I suggest to support functional programming in Ruby by making module methods called with <code>ModuleName::method_name</code> syntax raise an exception if the method uses instance or class variables (instance variables of the singleton class, of course).<br>
If i understand correctly, currently <code>ModuleName::method_name</code> and <code>ModuleName.method_name</code> behave identically, so i propose that they be different:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">module</span> <span class="nn">M</span>
<span class="kp">module_function</span>
<span class="k">def</span> <span class="nf">f</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="n">x</span><span class="o">*</span><span class="n">x</span>
<span class="k">end</span>
<span class="k">def</span> <span class="nf">g</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="vi">@x</span> <span class="o">||=</span> <span class="n">x</span>
<span class="vi">@x</span><span class="o">*</span><span class="vi">@x</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="no">M</span><span class="p">.</span><span class="nf">f</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span> <span class="c1"># => 4</span>
<span class="no">M</span><span class="p">.</span><span class="nf">g</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span> <span class="c1"># => 4</span>
<span class="no">M</span><span class="o">::</span><span class="n">f</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span> <span class="c1"># => 9</span>
<span class="no">M</span><span class="o">::</span><span class="n">g</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span> <span class="c1"># => Error: instance variable `@x` used in a functional call `M::g`</span>
</code></pre>
<p>Current behavior:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="no">M</span><span class="p">.</span><span class="nf">f</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span> <span class="c1"># => 4</span>
<span class="no">M</span><span class="p">.</span><span class="nf">g</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span> <span class="c1"># => 4</span>
<span class="no">M</span><span class="o">::</span><span class="n">f</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span> <span class="c1"># => 9</span>
<span class="no">M</span><span class="o">::</span><span class="n">g</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span> <span class="c1"># => 4</span>
</code></pre> Ruby master - Feature #6721 (Closed): Object#yield_selfhttps://redmine.ruby-lang.org/issues/67212012-07-11T16:35:21Zalexeymuranov (Alexey Muranov)
<p>I think the following method is missing from Ruby:</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">yield_self</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">)</span>
<span class="k">yield</span><span class="p">(</span><span class="nb">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">)</span>
<span class="k">end</span>
<span class="k">end</span>
</code></pre>
<p>I do not know a good use case, but it looks very natural to me. It can be used in method chains.</p>
<p>What do you think? Is there an alternative?</p> Ruby master - Feature #6717 (Rejected): Method like #instance_eval that returns self (like #tap)https://redmine.ruby-lang.org/issues/67172012-07-10T20:58:51Zalexeymuranov (Alexey Muranov)
<p>=begin<br>
How about adding a method that acts like (({#instance_eval})), but returns (({self}))?</p>
<p>Call it (({Object#instance_tap})) for example, or (({Object#tweak!})):</p>
<p>class Object<br>
def tweak!(*args, &block)<br>
instance_eval(*args, &block); self<br>
end<br>
end</p>
<p>This can be used to initialize a hash. Instead of:</p>
<p>room_numbers = (1..10).each_with_object({}) { |n, o| o[n] = 100 + n }</p>
<p>one would write:</p>
<p>room_numbers = {}.tweak! { (1..10).each { |n| self[n] = 100 +n } }</p>
<p>Googling for this kind of method gave me this:<br>
<a href="http://yuroyoro-blog.tumblr.com/post/24113917395/ruby-tap-instance-eval-tapeval" class="external">http://yuroyoro-blog.tumblr.com/post/24113917395/ruby-tap-instance-eval-tapeval</a><br>
=end</p> Ruby master - Feature #6375 (Rejected): Python notation for literal Hashhttps://redmine.ruby-lang.org/issues/63752012-04-29T02:44:29Zalexeymuranov (Alexey Muranov)
<p>Is there a reason not to use Python notation for literal Hash as an alternative to Ruby notation? I would like to be able to write either { 'a' => 'b', 'c' => 'd' } or { 'a' : 'b', 'c' : 'd' }, and either { :a => 'b' } or { :a: 'b' } .</p>
<p>I find { a: 'b' } to be a confusing alternative to { :a => 'b' }, and do not use it because of this (because it cannot be used if the key is not a symbol).</p> Ruby master - Feature #5555 (Rejected): rename #include? to #includes?https://redmine.ruby-lang.org/issues/55552011-11-03T07:28:40Zalexeymuranov (Alexey Muranov)
<p>Shouldn't the #include? method be renamed to #includes? ?<br>
I think this will be closer to correct English and consistent with #has_key? method (not #have_key?).</p> Ruby master - Feature #5534 (Rejected): Redefine Range class and introduce RelativeNumeric and Re...https://redmine.ruby-lang.org/issues/55342011-11-01T20:04:22Zalexeymuranov (Alexey Muranov)
<p>I started by commenting on Feature <a class="issue tracker-2 status-6 priority-4 priority-default closed" title="Feature: Inconsistent Array.slice() (Rejected)" href="https://redmine.ruby-lang.org/issues/4541">#4541</a>, but ended up with proposing a new feature myself.</p>
<p>I suggest to redefine the behavior of Range class so that all empty ranges be equal:</p>
<p>(2..1) == (1..-1) and (2..1) == (1...1) and (2..1) == ('z'..'a') # => true</p>
<p>In other fords, ranges <code>r1</code> and <code>r2</code> should be equal if and only if <code>r1.include?</code> and <code>r2.include?</code> give identical results for all inputs. (Why is it not <code>includes?</code> by the way?) Thus Range would simply be a way to store certain infinite sets.</p>
<p>This change will result in not being able to slice an array <code>a</code> from beginning and from the end simultaneously with <code>a[1..-2]</code>. To resolve this, i propose to introduce <code>RelativeNumeric</code> and <code>RelativeRange</code> classes.</p>
<p>Each <code>RelativeNumeric</code> would be a <code>Numeric</code> with an "anchor", which is an arbitrary symbol. For example:</p>
<p>3.from(:bottom) # would return a "relative" 3 with "anchor" :bottom</p>
<p>One can define shortcuts <code>#from_bottom</code> for <code>#from(:bottom)</code> and <code>#from_top</code> for <code>#from_top</code>.</p>
<p>A <code>RelativeRange</code> is a range with relative bounds. If bounds of a relative range r are relative to the same anchor and the range is seen to be empty, it should be equal to <em>the</em> empty relative range with this anchor. For example:</p>
<p>(3.from(:center)..2.from(:center)) == (0.from(:center)...0.from(:center)) # => true</p>
<p>Now, to do what is currently done by <code>a[1..-2]</code>, one can redefine <code>Array#slice</code> to use instead:</p>
<p>a[1.from_bottom..(-1).from_top]</p>
<p>What do you think?</p> Ruby master - Feature #5123 (Rejected): Alias Hash 1.9 as OrderedHashhttps://redmine.ruby-lang.org/issues/51232011-07-31T16:19:08Zalexeymuranov (Alexey Muranov)
<p>I read that it was a controversial decision to make Hash in Ruby 1.9 ordered.<br>
It is not clear if the present implementation is the best possible.<br>
I would like to express my thought.</p>
<p>It would be nice if the ordered Hash in Ruby 1.9 was aliased as OrderedHash.<br>
That way people who rely on preserving the insertion order in some application (me, for example) could explicitly use OrderedHash, and developers of Ruby would be free to redefine Hash in future versions if a better implementation that a doubly-linked circular list is found.<br>
(I read something about a possibility of using "red–black tree".)</p>
<p>Thanks.</p>
<p>Alexey Muranov.</p> Ruby master - Feature #5120 (Rejected): String#split needs to be logicalhttps://redmine.ruby-lang.org/issues/51202011-07-31T01:12:48Zalexeymuranov (Alexey Muranov)
<p>Here are examples showing a surprising and inconsistent behavior of <code>String#split</code> method:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="s2">"aa"</span><span class="p">.</span><span class="nf">split</span><span class="p">(</span><span class="s1">'a'</span><span class="p">)</span> <span class="c1"># => []</span>
<span class="s2">"aab"</span><span class="p">.</span><span class="nf">split</span><span class="p">(</span><span class="s1">'a'</span><span class="p">)</span> <span class="c1"># => ["", "", "b"]</span>
<span class="s2">"aaa"</span><span class="p">.</span><span class="nf">split</span><span class="p">(</span><span class="s1">'aa'</span><span class="p">)</span> <span class="c1"># => ["", "a"] </span>
<span class="s2">"aaaa"</span><span class="p">.</span><span class="nf">split</span><span class="p">(</span><span class="s1">'aa'</span><span class="p">)</span> <span class="c1"># => []</span>
<span class="s2">"aaaaa"</span><span class="p">.</span><span class="nf">split</span><span class="p">(</span><span class="s1">'aa'</span><span class="p">)</span> <span class="c1"># => ["", "", "a"] </span>
<span class="s2">""</span><span class="p">.</span><span class="nf">split</span><span class="p">(</span><span class="s1">''</span><span class="p">)</span> <span class="c1"># => []</span>
<span class="s2">"a"</span><span class="p">.</span><span class="nf">split</span><span class="p">(</span><span class="s1">''</span><span class="p">)</span> <span class="c1"># => ["a"]</span>
</code></pre>
<p>What is the definition of <code>split</code>? I would suggest something like this:</p>
<p><code>str1.split(str2)</code> returns the maximal array of non-empty substrings of <code>str1</code> that can be concatenated with copies of <code>str2</code> to form <code>str1</code>.</p>
<p>Addition can be made to this definition to make clear what to expect as the result of <code>"baaab".split("aa")</code>.</p>