[ruby-core:96364] [Ruby master Misc#16436] hash missing #last method, make it not so consistent (it has first)

[email protected]
Newsgroups gmane.comp.lang.ruby.core
Message-ID <redmine.journal-83274.20191220031830.1620c815faceb134@ruby-lang.org>
Issue #16436 has been updated by zw963 (Wei Zheng).


zw963 (Wei Zheng) wrote:
> ``` ruby
> (pry):main(0)> h = {x:1, y:2, z:3}
> {
>     :x => 1,
>     :y => 2,
>     :z => 3
> }
> (pry):main(0)> h.first
> [
>     :x,
>     1
> ]
> (pry):main(0)> h.first.first
> :x
> (pry):main(0)> h.first.last
> 1
> ```
> 
> last method not exist.
> 
> ```ruby
> (pry):main(0)> h.last
> 
> Exception: NoMethodError: undefined method `last' for {:x=>1, :y=>2, :z=>3}:Hash
> ```
> 
> We have to use #to_a to make last working.
> 
> ```ruby
> (pry):main(0)> h.to_a.last
> [
>     :z,
>     3
> ]
> 
> (pry):main(0)> h.to_a.last.first
> :z
> (pry):main(0)> h.to_a.last.last
> 3
> ```



sawa (Tsuyoshi Sawada) wrote:
> What is this issue? Is it just a note to everyone, or are you claiming this to be a bug, or is it a feature request?

Sorry, this issue not a bug, i knew Hash#first come from Enumerable, the reason i issue it is:  i love ruby language, use it days and days, 
when encountering some inconsistencies things, i expect to give some feedback, and what i thought.

So, for this issue, the question is:

why Array include Enumerable module,  and implement Array#first Array#last method, if we should do same things for Hash?
make this language more elegant?



----------------------------------------
Misc #16436: hash missing #last method, make it not so consistent (it has first)
https://bugs.ruby-lang.org/issues/16436#change-83274

* Author: zw963 (Wei Zheng)
* Status: Open
* Priority: Normal
* Assignee: 
----------------------------------------
``` ruby
(pry):main(0)> h = {x:1, y:2, z:3}
{
    :x => 1,
    :y => 2,
    :z => 3
}
(pry):main(0)> h.first
[
    :x,
    1
]
(pry):main(0)> h.first.first
:x
(pry):main(0)> h.first.last
1
```

last method not exist.

```ruby
(pry):main(0)> h.last

Exception: NoMethodError: undefined method `last' for {:x=>1, :y=>2, :z=>3}:Hash
```

We have to use #to_a to make last working.

```ruby
(pry):main(0)> h.to_a.last
[
    :z,
    3
]

(pry):main(0)> h.to_a.last.first
:z
(pry):main(0)> h.to_a.last.last
3
```






-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:[email protected]?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.