Project

General

Profile

Actions

Feature #19466

closed

Class.new takes a block, why doesn't Module.new take a block?

Added by ioquatix (Samuel Williams) about 1 year ago. Updated about 1 year ago.

Status:
Closed
Assignee:
-
Target version:
-
[ruby-core:112585]

Description

Class.new do
  #... equivalent to class_eval
end

So, why don't we introduce:

Module.new do
  #... equivalent to class_eval
end

Updated by sawa (Tsuyoshi Sawada) about 1 year ago

It does.

A = Module.new do
  def foo; "foo" end
end
class B; include A end
B.new.foo # => "foo"

Cf. https://ruby-doc.org/3.2.1/Module.html#method-c-new

Updated by ioquatix (Samuel Williams) about 1 year ago

Ahh, I tried this but I must have made a mistake. So why does EnvUtil not use this? Maybe it's legacy?

Updated by ioquatix (Samuel Williams) about 1 year ago

  • Status changed from Open to Closed

Okay, I confirmed it's working by my own hand, haha. I must work too hard this week, my brain is not functioning correctly.

I updated the PR to take advantage of that. In fact, it was right in front of me the whole time.

https://github.com/ruby/ruby/pull/7376/files#diff-84e45206777a1891b7ce021458bfc2fa99cc6cf5f74f9370b883f2ad15eb25a1R266

Actions

Also available in: Atom PDF

Like0
Like1Like0Like1