Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why isn't [1..5] == [1,2,3,4,5]?

Why isn't [1..5] == [1,2,3,4,5]?

Why isn't [1..5].to_a == [1,2,3,4,5]?

How to convert from [1..5] to [1,2,3,4,5]?

like image 260
B Seven Avatar asked Sep 10 '12 01:09

B Seven


People also ask

Why is Collatz conjecture unsolved?

And since Collatz orbits always decrease at even numbers, this suggests that all Collatz sequences must decrease in the long run. This probabilistic argument is widely known, but no one has been able to extend it to a complete proof of the conjecture.

Why is 3x1 impossible?

The 3x+1 problem concerns an iterated function and the question of whether it always reaches 1 when starting from any positive integer. It is also known as the Collatz problem or the hailstone problem. . This leads to the sequence 3, 10, 5, 16, 4, 2, 1, 4, 2, 1, ... which indeed reaches 1.

What is the hardest math problem?

The Riemann Hypothesis, famously called the holy grail of mathematics, is considered to be one of the toughest problems in all of mathematics.

Is there a math problem that Cannot be solved?

The Collatz Conjecture is the simplest math problem no one can solve — it is easy enough for almost anyone to understand but notoriously difficult to solve. So what is the Collatz Conjecture and what makes it so difficult?


2 Answers

[1..5] is an array which only has one element, the range object 1..5

[1..5].to_a is still [1..5]

(1..5).to_a is [1,2,3,4,5]

like image 71
xdazz Avatar answered Sep 17 '22 18:09

xdazz


[1..5] is an array with one element - a range object, all attempts to iterate through it will fail. array can have many kinds of objects in them. In my example above I treat the range as just a range and make any array from it directly.

1.9.3-p125 :008 > (1..5).to_a  # Note regular parens.
 => [1, 2, 3, 4, 5] 
1.9.3-p125 :009 > 

To do more closely what you stated - starting with [1..5] - you could do:

1.9.3-p125 :013 > newarray = []

1.9.3-p125 :014 > [1..5][0].each {|e| newarray << e}
 => 1..5 
1.9.3-p125 :015 > newarray
 => [1, 2, 3, 4, 5] 
1.9.3-p125 :016 > 
like image 37
Michael Durrant Avatar answered Sep 17 '22 18:09

Michael Durrant