Project

General

Profile

Actions

Feature #12165

open

Hash#first, Hash#last

Added by stillhart (Fabian Stillhart) about 8 years ago. Updated 11 months ago.

Status:
Open
Assignee:
-
Target version:
-
[ruby-core:74268]

Description

Just run into a simple problem with a colleague and was wondering why there is no Hash#last method?

{a: true, b: false}.last
NoMethodError: undefined method 'last' for {:a=>true, :b=>false}:Hash

Interestingly I while playing arround I found out that the Hash#first method works. But why is it not in the ruby-doc?

{a: true, b: false}.first
=> [:a, true]

I would assume the Hash#last method would work like the Hash#first method:

{a: true, b: false}.last
=> [:b, false]

If I am not wrong the order of a Hash is always the same when calling Hash#each. So wouldn't it make sense to have Hash#last method?

I tested it in Ruby 2.2.4 and Ruby 2.3.0.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0