Frage

Ich bin neu in Rubin.Gibt es eine Möglichkeit, ein einzelnes 'Def-Array / Range / Hash' mit einer Methode_missing zu schreiben, die für alle Bereiche, Arrays und Hash d. H. Alle ENUMERABLE ?Für z.B.Folgendes sollte funktionieren: generasacodicetagpre.

sum () ist unsere benutzerdefinierte Methode definiert in innenmethode_missing.Ab sofort habe ich drei verschiedene Funktionen geschrieben, die Method_missing für Array, Reichweite und HASH separat umgehen.

War es hilfreich?

Lösung

You can define method_missing for all Enumerables by opening Enumerable module and adding an instance method to it:

module Enumerable
  def method_missing *args
    puts 'hi'
  end
end

[].hithere # => hi

But I would recommend to define concrete sum method (starts with lower case letter, according to Ruby methods naming guidelines) instead of adding this logic to method_missing:

module Enumerable
  def sum
    # your implementation
  end
end

[].sum

Andere Tipps

As Alex has said the best way would be to just define your own method #sum for Enumerable, however while each Enumerable is supposed to have #each, what it yields is different for each subclass. For example Hash yields a key-value pair, which your single #sum method would have to know how to deal with.

Here's one example of how it might do it, it tries to splat the incoming arguments into an array and the sum includes only the last element of the array. This works for Hashes, Array and Ranges (however it probably only works in 1.9)

module Enumerable
  def sum
    r = 0
    each {|(*x)| r += x.last}
    r
  end
end

Alternatively you could define your own iterating method for each Enumerable, like #each_for_sum and let #sum use that method to do the summing. But you'd probably have to write several versions of #each_for_sum for different Enumerables (for example, the default #each_for_sum would be just Enumerable#each, but Hash#each_for_sum would yield just values).

module Enumerable
  def sum
    r = 0
    each_for_sum {|x| r += x}
    r
  end

  def each_for_sum &block
     each(&block)
  end
end

class Hash
  def each_for_sum
    values.each {|x| yield x}
  end
end
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top