Project

General

Profile

Actions

Bug #16609

closed

`#sample` not working with blocks properly

Bug #16609: `#sample` not working with blocks properly

Added by LukeH7789 (Luke Hill) about 6 years ago. Updated about 6 years ago.

Status:
Closed
Assignee:
-
Target version:
-
ruby -v:
2.7.0 / 2.6.5
[ruby-core:97064]

Description

This may be a bug, it may be an uncoded/partially working feature. Running #sample with a block doesn't complain or throw an error; The following code runs, but doesn't execute correctly.

a = [1, 2, 3, 4, 5, 6]
a.sample(&:even?) #=> 1 or 3 or 5 is **expected**
a.sample { |e| e.even? } #=> 1 or 3 or 5 is **expected**

But if you run the second snippet, you can get any number from 1 to 6. Here are a few runs:

a.sample
# => 3
a.sample(&:even?)
# => 1
a.sample(&:even?)
# => 2
a.sample(&:even?)
# => 6
a.sample(&:even?)
# => 3
a.sample(&:even?)
# => 2
Actions

Also available in: PDF Atom