Project

General

Profile

Feature #6336

Allow Ruby hacker to extend a module or a class, without needing to know if it is a module or a class

Added by shevegen (Robert A. Heiler) over 7 years ago. Updated over 7 years ago.

Status:
Rejected
Priority:
Normal
Assignee:
-
Target version:
-
[ruby-core:44515]

Description

Hi,

Please correct me if I am wrong.

I think in order to extend a module or a class, you must do
something like this (first, two examples, one for module,
one for a class):

module Foo
def test
puts 'This is test from module Foo.'
end
end

class Bar
def test
puts 'This is test from class Bar.'
end
end

Now to modify the behaviour:

module Foo
def test
puts 'This is test from module Foo, but modified.'
end
end

class Bar
def test
puts 'This is test from class Bar, but modified.'
end
end

My question is, why does the Ruby hacker need to know that he
modifies a class or a module? It feels like a (small) obstacle
for no real gain I can see.

I would propose something else like:

extend Bar
def test
puts 'We now modified Bar but we don't care whether it is a module or a class.'
end
end

I am not sure about "extend" as keyword. I thought about other keywords, but I could
not find good ones. So I also have to admit that sticking to "class" and "module" as
we do right now is not a bad solution - because we don't need any new word.

Still I wanted to suggest this, so that I can understand the design decision better.

Thank you for reading.


Related issues

Is duplicate of Ruby master - Bug #6335: Allow Ruby hacker to extend a module or a class, without needing to know if it is a module or a classRejected04/22/2012Actions

History

Updated by shevegen (Robert A. Heiler) over 7 years ago

What I meant to say is - I think adding a new keyword would not be a good
solution.

Updated by mame (Yusuke Endoh) over 7 years ago

  • Status changed from Open to Rejected

I don't know why do you want to do such a thing, but you can use Module#module_eval for both class and module:

Foo.module_eval do
def test
puts 'This is test from module Foo, but modified.'
end
end
Bar.module_eval do
def test
puts 'This is test from module Bar, but modified.'
end
end

Adding a keyword for this requires a much more good reason.

--
Yusuke Endoh mame@tsg.ne.jp

Also available in: Atom PDF