Project

General

Profile

Actions

Feature #15374

closed

Proposal: Enable refinements to `#method_missing`

Added by osyo (manga osyo) almost 6 years ago. Updated over 1 year ago.

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

Updated by Hanmac (Hans Mackowiak) almost 6 years 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?

Updated by osyo (manga osyo) almost 6 years ago

oops, that's right.

Updated by Eregon (Benoit Daloze) over 5 years ago

This wouldn't work, e.g. for hash.first, or any existing method of Hash.

Can you show a use case for this feature?

Updated by osyo (manga osyo) over 5 years ago

Yes, it will be the specification of Ruby.
method_missing has a large side effect.
So can use using to control by context.

module HashWithAsscessKeyToMethod
  refine Hash do
      # name is Symbol or String
      def method_missing(name)
          self[name] || self[name.to_s]
      end
  end
end


# Do not want to use Hash#method_missing
hash = { name: "homu", age: 14 }
pp hash[:name]    # OK
# pp hash.name    # NG


# Do want to use Hash#method_missing
using HashWithAsscessKeyToMethod
pp hash.name      # OK

User can choose.

Updated by matz (Yukihiro Matsumoto) over 5 years ago

  • Status changed from Open to Rejected

I don't see any real-world usage of allowing #method_missing refinable. Maybe it can be used only for tricks and obfusticated code.

Matz.

Updated by osyo (manga osyo) over 5 years ago

OK, Thanks matz :)

Updated by shreeve (Steve Shreeve) over 1 year ago

I don't see any real-world usage of allowing #method_missing refinable. Maybe it can be used only for tricks and obfusticated code.

We use this heavily and it would be great if method_missing could be refinable.

Here's an example:

class Hash
  alias_method :default_lookup, :[]

  def [](key, miss=nil)
    key?(key) and return default_lookup(key) || miss

    ary = key.to_s.split(/(?:[.\/\[]|\][.\/]?)/)
    val = ary.inject(self) do |obj, sub|
      if    obj == self        then default_lookup(sub.to_sym)
      elsif obj == nil         then break
      elsif sub =~ /\A-?\d*\z/ then obj[sub.to_i]
      else                          obj[sub.to_sym]
      end
    end or miss
  end

  def method_missing(name, *args)
    name =~ /=$/ ? send(:[]=, $`.to_sym, *args) : send(:[], name, *args)
  end
end

book = {
  name: "Ruby Object Model",
  url: [
    "https://google.com",
    "https://pepsi.com",
    "https://byu.edu",
  ],
  team: {
    boss: {
      name: "Mark",
      age: 23,
    },
    janitor: {
      name: "Bob",
      age: 56,
      kids: %w[ Billy Sue Tim Nebo Dash ],
    },
  },
}

p book.name  # => "Ruby Object Model"
p book.color # => nil
p book.color("red") # => "red"
p book.url[2] # => "https://byu.edu"
p book["team/janitor/age"] # => 56
p book["team.janitor.age"] # => 56
p book["team/janitor/song", "None"] # => "None"
p book["team/janitor/kids[3]"] # => "Nebo"

This approach has been extremely helpful and useful, but I can't get it to work with refinements and I always feel a little dirty with bastardizing the core Hash class.

Can this code be made to work with refinements? This is a real world case, where we rely on this heavily for an api in production for several years. Refinement support would be great!

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0