Project

General

Profile

Actions

Feature #12734

closed

`Array#flat?`

Added by sos4nt (Stefan Schüßler) over 7 years ago. Updated over 7 years ago.

Status:
Closed
Assignee:
-
Target version:
-
[ruby-core:77205]

Description

It might be useful to have a method that determines whether an array is flat, i.e. one-dimensional.

[1, 2, 3].flat?   #=> true
[1, [2, 3]].flat? #=> false

The result should be equivalent to ary == ary.flatten (without the overhead of actually flattening the array)

Updated by nobu (Nobuyoshi Nakada) over 7 years ago

  • Status changed from Open to Feedback

Please explain the use-case.
It doesn't seem useful to me.

Updated by herwin (Herwin W) over 7 years ago

This is pretty trivial to implement yourself:

class Array
  def flat?
    none?{|e|e.is_a?(Array)}
  end
end
[1,2,3].flat?
=> true
[1,[2,3]].flat?
=> false

I have to agree with Nobuyoshi Nakada that I don't really see the use case here.

Updated by sos4nt (Stefan Schüßler) over 7 years ago

Herwin W wrote:

This is pretty trivial to implement yourself:

Oh, you're right. I didn't realize that this is merely a type check.

May someone close this feature request?

Actions #4

Updated by shyouhei (Shyouhei Urabe) over 7 years ago

  • Status changed from Feedback to Closed

OK, closing as per request.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0