Project

General

Profile

Feature #15374

Proposal: Enable refinements to `#method_missing`

Added by osyo (manga osyo) 10 days ago. Updated 9 days ago.

Status:
Open
Priority:
Normal
Assignee:
-
Target version:
-
[ruby-core:90258]

Description

Proposal enable refinements to #method_missing.
It can be used in the following cases.

# Access key value with method
using Module.new {
    refine Hash do
        # name is Symbol or String
        def method_missing(name)
            self[name.to_sym] || self[name.to_s]
        end
    end
}

hash = { name: "homu", "age" => 14 }
pp hash.name
# => "homu"
pp hash.age
# => "age"

method_missing is hard hacking.
I would like to use Refinements with method_missing.

pull request: https://github.com/ruby/ruby/pull/2036

History

#1 [ruby-core:90265] Updated by Hanmac (Hans Mackowiak) 10 days ago

i always like the fun you can do with method_missing, but for your example, method_missing always use a symbol for the name, so name.to_sym should just return self or did you do that on purpose?

#2 [ruby-core:90283] Updated by osyo (manga osyo) 9 days ago

oops, that's right.

Also available in: Atom PDF