Gewusst wie: übergeben von Parametern an einen proc beim Aufruf von einer Methode?
proc = Proc.new do |name|
puts "Thank you #{name}!"
end
def thank
yield
end
proc.call # output nothing, just fine
proc.call('God') # => Thank you God!
thank &proc # output nothing, too. Fine;
thank &proc('God') # Error!
thank &proc.call('God') # Error!
thank proc.call('God') # Error!
# So, what should I do if I have to pass the 'God' to the proc and use the 'thank' method at the same time ?
Dank 🙂
InformationsquelleAutor Croplio | 2010-08-04
Du musst angemeldet sein, um einen Kommentar abzugeben.
Ich denke, der beste Weg ist:
InformationsquelleAutor Daniel O'Hara
Dann können Sie tun:
Keine Notwendigkeit für
, &block
Lafortune: Du bist unter Bezugnahme auf die Definition von
thank
, nicht die Berufung, richtig?richtig, das ist, warum ich schrieb, dass die
, &block
nicht notwendig war und nicht, dass die, &proc
war (das ist erforderlich)Hoppla! Ich glaube, ich Las Sie als die gleiche Sache, weil Sie sich Reimen.
InformationsquelleAutor Nada Aldahleh
einer anderen Weise zu dem, was Nada vorgeschlagen (es ist das gleiche, nur andere syntax):
Funktioniert es, aber ich mag es nicht. Dennoch wird es den Lesern zu verstehen, WIE procs und Blöcke benutzt werden.
InformationsquelleAutor BKSpurgeon