Project

General

Profile

Actions

Feature #14498

closed

Class#to_proc

Added by Nondv (Dmitry Non) almost 7 years ago. Updated almost 7 years ago.

Status:
Rejected
Assignee:
-
Target version:
-
[ruby-core:85694]

Description

Hello!

I was creating a list of class instances via some_list.map { |e| SomeClass.new(e) } and wondered if there's a Class#to_proc to make possible doing some_list.map(&SomeClass).
Well, there is not.

Basically, this is what I suggest:

class Class
  def to_proc
    proc { |*args| self.new(*args) }
  end
end

class Dog
  attr_reader :name

  def initialize(name)
    @name = name
  end
end

names = %w[Lucky Pluto Balto]
names.map(&Dog) # map names to dogs

Here's the pull request:

https://github.com/ruby/ruby/pull/1821


Related issues 1 (0 open1 closed)

Related to Ruby master - Feature #4910: Classes as factoriesRejectedmatz (Yukihiro Matsumoto)Actions
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0