Project

General

Profile

Feature #14498

Class#to_proc

Added by Nondv (Dmitriy Non) about 2 months ago. Updated about 2 months ago.

Status:
Rejected
Priority:
Normal
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

Related to Ruby trunk - Feature #4910: Classes as factoriesRejected

History

#1 Updated by Nondv (Dmitriy Non) about 2 months ago

  • Description updated (diff)

#2 [ruby-core:85696] Updated by Hanmac (Hans Mackowiak) about 2 months ago

  • Description updated (diff)

Duplicate of #4910

PS: might be a bit more complicated in C,
but i would like this:

 proc { |*args, **keyargs| self.new(*args, **keyargs) }

so it works with keyargs too

#3 [ruby-core:85698] Updated by Nondv (Dmitriy Non) about 2 months ago

Hanmac (Hans Mackowiak) wrote:

Duplicate of #4910

PS: might be a bit more complicated in C,
but i would like this:

 proc { |*args, **keyargs| self.new(*args, **keyargs) }

so it works with keyargs too

Hi!
It's not really a duplicate since there is a different suggestion.

I have written PR in C based on Hash#to_proc definition

#4 [ruby-core:85708] Updated by matz (Yukihiro Matsumoto) about 2 months ago

  • Status changed from Open to Rejected

Nice try. But I think it's too concise that could lead to unreadable code.

Matz.

#5 Updated by matz (Yukihiro Matsumoto) about 2 months ago

Also available in: Atom PDF