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

Updated by LukeH7789 (Luke Hill) about 6 years ago Actions #1

  • ruby -v changed from 2.7.0 to 2.7.0 / 2.6.5

Updated by matz (Yukihiro Matsumoto) about 6 years ago Actions #2 [ruby-core:97065]

  • Status changed from Open to Closed

#sample do not take blocks. They are just ignored. It's one of the limitations of Ruby language.

Matz.

Updated by nobu (Nobuyoshi Nakada) about 6 years ago Actions #3

  • Subject changed from #select not working with blocks properly to `#sample` not working with blocks properly

Updated by sawa (Tsuyoshi Sawada) about 6 years ago Actions #4

  • Description updated (diff)
Actions

Also available in: PDF Atom