Project

General

Profile

Actions

Feature #10365

closed

`Rational#to_a` and `Array#to_r`

Added by sawa (Tsuyoshi Sawada) over 9 years ago. Updated over 9 years ago.

Status:
Rejected
Assignee:
-
Target version:
-
[ruby-core:65604]

Description

I guess people would want to extract the numerator and the denominator of an array. to_a would perhaps be a good name:

(2/51).to_a # => [2, 51]

It also might be useful if Array#to_r were provided as an alternative way to create a rational from an array:

[4, 6].to_r #=> (2/3)

This would be equivalent to Rational(*[4, 6]).

Updated by sawa (Tsuyoshi Sawada) over 9 years ago

Sorry, I meant

2/51r.to_a # => [2, 51]

Updated by nobu (Nobuyoshi Nakada) over 9 years ago

IMHO, splatting a rational doesn't feel to make sense, so to_a doesn't feel appropriate.

Updated by jeremyevans0 (Jeremy Evans) over 9 years ago

Wouldn't the proposed Rational#to_a break backwards compatibility, as well as consistency with other numeric classes:

Currently:

Array(1) # => [1]
Array(1.0) # => [1.0]
Array(BigDecimal.new('1.0')) # => [#BigDecimal:12c032467878,'0.1E1',9(18)]
Array(Complex(1, 2)) => [(1+2i)]
Array(1/2r) # => [(1/2)]

With proposed method:

Array(1/2r) # => [1, 2]

Updated by sawa (Tsuyoshi Sawada) over 9 years ago

I think Nobuyoshi Nakada and Jeremy Evans are right. I will withdraw to_a from the proposal.

Updated by matz (Yukihiro Matsumoto) over 9 years ago

  • Status changed from Open to Rejected

And without having Rational#to_a, Array#to_r does not mean much.
So I reject this.

Matz.

Updated by nobu (Nobuyoshi Nakada) over 9 years ago

Just a memo:

[1, 2].inject(:quo) #=> (1/2)

Updated by sawa (Tsuyoshi Sawada) over 9 years ago

Nobuyoshi Nakada wrote:

Just a memo:

[1, 2].inject(:quo) #=> (1/2)

Thanks for the information.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0