Skip to main content
added 36 characters in body
Source Link
Stefan Pochmann
  • 29k
  • 9
  • 48
  • 117

As the other answers explain in more detail, your when [1..3 && 2] doesn't work because that's actually when [1..2] and because arrays don't compare their elements with === (which when does and which the range would need to do).

Here's another way to make it work, by fixing exactly those two issues.

First, use [1..3, 2] instead of [1..3 && 2], so the two conditions don't get combined but stay separated in the array. Then, to get === used, create a subclass of Array that compares elements with === instead of ==. And use it in the when condition instead of a normal array. Full code:

roll1 = rand(1..6) roll2 = rand(1..2) class Case < Array def ===(other) zip(other).all? { |x, y| x === y } end end result = case[roll1, roll2] when Case[1..3, 1] "Low / Low" when Case[4..6, 1] "High / Low" when Case[1..3, 2] "Low / High" when Case[4..6, 2] "JACKPOT!!" end puts roll1, roll2, result 

That for example prints:

6 2 JACKPOT!! 

I guess whether this is good / worth it for you depends on your actual use case. But I like it. And as a Ruby beginner myself, this little exercise helped me understand better how when and === work.

ThisAlso see the discussion under @muistooshort's answer for some thoughts about this.

And this answer about what === does was also very illuminating:
https://stackoverflow.com/a/3422349/1672429

As the other answers explain in more detail, your when [1..3 && 2] doesn't work because that's actually when [1..2] and because arrays don't compare their elements with === (which when does and which the range would need to do).

Here's another way to make it work.

First, use [1..3, 2] instead of [1..3 && 2], so the two conditions don't get combined but stay separated in the array. Then, to get === used, create a subclass of Array that compares elements with === instead of ==. And use it in the when condition instead of a normal array. Full code:

roll1 = rand(1..6) roll2 = rand(1..2) class Case < Array def ===(other) zip(other).all? { |x, y| x === y } end end result = case[roll1, roll2] when Case[1..3, 1] "Low / Low" when Case[4..6, 1] "High / Low" when Case[1..3, 2] "Low / High" when Case[4..6, 2] "JACKPOT!!" end puts roll1, roll2, result 

That for example prints:

6 2 JACKPOT!! 

I guess whether this is good / worth it for you depends on your actual use case. But I like it. And as a Ruby beginner myself, this little exercise helped me understand better how when and === work.

This answer about what === does was also very illuminating:
https://stackoverflow.com/a/3422349/1672429

As the other answers explain in more detail, your when [1..3 && 2] doesn't work because that's actually when [1..2] and because arrays don't compare their elements with === (which when does and which the range would need to do).

Here's another way to make it work, by fixing exactly those two issues.

First, use [1..3, 2] instead of [1..3 && 2], so the two conditions don't get combined but stay separated in the array. Then, to get === used, create a subclass of Array that compares elements with === instead of ==. And use it in the when condition instead of a normal array. Full code:

roll1 = rand(1..6) roll2 = rand(1..2) class Case < Array def ===(other) zip(other).all? { |x, y| x === y } end end result = case[roll1, roll2] when Case[1..3, 1] "Low / Low" when Case[4..6, 1] "High / Low" when Case[1..3, 2] "Low / High" when Case[4..6, 2] "JACKPOT!!" end puts roll1, roll2, result 

That for example prints:

6 2 JACKPOT!! 

I guess whether this is good / worth it for you depends on your actual use case. But I like it. And as a Ruby beginner myself, this little exercise helped me understand better how when and === work.

Also see the discussion under @muistooshort's answer for some thoughts about this.

And this answer about what === does was also very illuminating:
https://stackoverflow.com/a/3422349/1672429

added 6 characters in body
Source Link
Stefan Pochmann
  • 29k
  • 9
  • 48
  • 117

As the other answers explain in more detail, your when [1..3 && 2] doesn't work because that's actually when [1..2] and because arrays don't compare their elements with === (which when does and which the range would need to do).

Here's another way to make it work.

First, use [1..3, 2] instead of [1..3 && 2], so the two conditions don't get combined but stay separated in the array. Then, to get === used, create a subclass of Array that compares elements with === instead of ==. And use it in the when condition instead of ana normal array. Full code:

roll1 = rand(1..6) roll2 = rand(1..2) class Case < Array def ===(other) zip(other).all? { |x, y| x === y } end end result = case[roll1, roll2] when Case[1..3, 1] "Low / Low" when Case[4..6, 1] "High / Low" when Case[1..3, 2] "Low / High" when Case[4..6, 2] "JACKPOT!!" end puts roll1, roll2, result 

That for example prints:

6 2 JACKPOT!! 

I guess whether this is good / worth it for you depends on your actual use case. But I like it. And as a Ruby beginner myself, this little exercise helped me understand better how when and === work.

This answer about what === does was also very illuminating:
https://stackoverflow.com/a/3422349/1672429

As the other answers explain in more detail, your when [1..3 && 2] doesn't work because that's actually when [1..2] and because arrays don't compare their elements with === (which when does and which the range would need to do).

Here's another way to make it work.

First, use [1..3, 2] instead of [1..3 && 2], so the two conditions don't get combined but stay separated in the array. Then, to get === used, create a subclass of Array that compares elements with === instead of ==. And use it in the when condition instead of an array. Full code:

roll1 = rand(1..6) roll2 = rand(1..2) class Case < Array def ===(other) zip(other).all? { |x, y| x === y } end end result = case[roll1, roll2] when Case[1..3, 1] "Low / Low" when Case[4..6, 1] "High / Low" when Case[1..3, 2] "Low / High" when Case[4..6, 2] "JACKPOT!!" end puts roll1, roll2, result 

That for example prints:

6 2 JACKPOT!! 

I guess whether this is good / worth it for you depends on your actual use case. But I like it. And as a Ruby beginner myself, this little exercise helped me understand better how when and === work.

As the other answers explain in more detail, your when [1..3 && 2] doesn't work because that's actually when [1..2] and because arrays don't compare their elements with === (which when does and which the range would need to do).

Here's another way to make it work.

First, use [1..3, 2] instead of [1..3 && 2], so the two conditions don't get combined but stay separated in the array. Then, to get === used, create a subclass of Array that compares elements with === instead of ==. And use it in the when condition instead of a normal array. Full code:

roll1 = rand(1..6) roll2 = rand(1..2) class Case < Array def ===(other) zip(other).all? { |x, y| x === y } end end result = case[roll1, roll2] when Case[1..3, 1] "Low / Low" when Case[4..6, 1] "High / Low" when Case[1..3, 2] "Low / High" when Case[4..6, 2] "JACKPOT!!" end puts roll1, roll2, result 

That for example prints:

6 2 JACKPOT!! 

I guess whether this is good / worth it for you depends on your actual use case. But I like it. And as a Ruby beginner myself, this little exercise helped me understand better how when and === work.

This answer about what === does was also very illuminating:
https://stackoverflow.com/a/3422349/1672429

Source Link
Stefan Pochmann
  • 29k
  • 9
  • 48
  • 117

As the other answers explain in more detail, your when [1..3 && 2] doesn't work because that's actually when [1..2] and because arrays don't compare their elements with === (which when does and which the range would need to do).

Here's another way to make it work.

First, use [1..3, 2] instead of [1..3 && 2], so the two conditions don't get combined but stay separated in the array. Then, to get === used, create a subclass of Array that compares elements with === instead of ==. And use it in the when condition instead of an array. Full code:

roll1 = rand(1..6) roll2 = rand(1..2) class Case < Array def ===(other) zip(other).all? { |x, y| x === y } end end result = case[roll1, roll2] when Case[1..3, 1] "Low / Low" when Case[4..6, 1] "High / Low" when Case[1..3, 2] "Low / High" when Case[4..6, 2] "JACKPOT!!" end puts roll1, roll2, result 

That for example prints:

6 2 JACKPOT!! 

I guess whether this is good / worth it for you depends on your actual use case. But I like it. And as a Ruby beginner myself, this little exercise helped me understand better how when and === work.