0

That seems all the more unexpected when defining a dummy method passing all arguments do the job. That is the following works:

  def method_missing(ago, *lokatoj, &bloko)
    mistrafe(ago, *lokatoj, &bloko)
  end

  def mistrafe(ago, *lokatoj, &bloko)
    # faru ion
  end

While the following doen't

alias mistrafe method_missing

Why is that so?

psychoslave
  • 2,783
  • 3
  • 27
  • 44

1 Answers1

4

Generally speaking, you want method_missing (that is called by Ruby internally) to be an alias for mistrafe not vice versa. You have an implementation in mistrafe and you want reassign method_missing to be calling it.

That said, the following will work:

alias method_missing mistrafe

See the alias documentation.

3limin4t0r
  • 19,353
  • 2
  • 31
  • 52
Aleksei Matiushkin
  • 119,336
  • 10
  • 100
  • 160
  • Yes, it works. I'm astonished with my own confusion as with other methods aliases, I didn't made this error of inverting parameters. Now to be complete, for some reason in this case it seems required to put the alias *after* the method definition, when I can successfully alias other methods before there definition. Any idea why is that so? – psychoslave Jul 03 '18 at 08:34
  • Use [`Module#alias_method`](https://ruby-doc.org/core/Module.html#method-i-alias_method) to put it wherever. `alias` requires the method to exist due to it’s syntax. – Aleksei Matiushkin Jul 03 '18 at 08:36
  • 1
    But before you use `alias_method` over `alias`, first have a look at the differences and decide for yourself. https://stackoverflow.com/a/27310250/3982562 – 3limin4t0r Jul 03 '18 at 09:40
  • Understandably, `alias` can't be aliased as it's a language keyword, and no keyword can be aliased. But shouldn't it possible to alias `alias_method`? I tried to make `alias alivoke alias_method` and ended with `undefined method `alias_method' for module `Ĝue' (NameError)` – psychoslave Jul 03 '18 at 12:41
  • class Module alias_method :alivoke, :alias_method ;end – psychoslave Jul 03 '18 at 13:07
  • Thus said, @mudasobwa and @johan-wentholt even with `alias_method` it only works if it's placed after the `mistrafe` method. – psychoslave Jul 04 '18 at 02:59