Project

General

Profile

Actions

Feature #16341

open

Proposal: Set#to_proc and Hash#to_proc

Added by Nondv (Dmitry Non) over 4 years ago. Updated over 4 years ago.

Status:
Open
Assignee:
-
Target version:
-
[ruby-core:95789]

Description

class Set
  def to_proc
    -> (x) { include?(x) } # or method(:include?).to_proc
  end
end

Usage:

require 'set'

banned_numbers = Set[0, 5, 7, 9]
(1..10).reject(&banned_numbers) # ===> [1, 2, 3, 4, 6, 8, 10]

UPD

also for hash:

class Hash
  def to_proc
    ->(key) { self[key] }
  end
end

dogs = ['Lucky', 'Tramp', 'Lady']
favourite_food = { 'Lucky' => 'salmon', 'Tramp' => 'pasta', 'Lady' => 'pasta' }

food_to_order = dogs.map(&favourite_food)
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0