Project

General

Profile

Actions

Bug #18799

open

Refinement#import_methods vs attr_reader

Added by palkan (Vladimir Dementyev) about 1 month ago. Updated about 1 month ago.

Status:
Open
Priority:
Normal
Assignee:
-
Target version:
-
ruby -v:
ruby 3.1.1p18 (2022-02-18 revision 53f5fc4236) [x86_64-darwin21]
[ruby-core:108660]

Description

Currently, it's not possible to import methods defined as attr_{reader, accessor,writer}:

module M
  attr_reader :fuu
end

module R
  refine Object do
    import_methods M
  end
end

#=> Can't import method which is not defined with Ruby code: M#fuu (ArgumentError)

Is that a known/intentional limitation or a bug? From the error message it might seem that this is a bug (since attr_smth is a Ruby code).

Updated by palkan (Vladimir Dementyev) about 1 month ago

UPD: looks like define_method is not supported, too:

module M
  define_method(:fuu) { }
end

module R
  refine Object do
    import_methods M
  end
end

#=> Can't import method which is not defined with Ruby code: M#fuu (ArgumentError)

Updated by Eregon (Benoit Daloze) about 1 month ago

is not defined with Ruby code

It means the method's body is not Ruby code. And indeed, attr_* defines methods which are not defined in/with Ruby code.

So this seems expected.

Same for define_method, which has some internal wrapper around the given block and so cannot easily be import-ed.

Updated by shugo (Shugo Maeda) about 1 month ago

As eregon stated, it's an intended imitation.

It's possible to allow importing C methods without refinements activation.
It may be helpful for attr_*, but not for define_method.

Actions

Also available in: Atom PDF