folgende bedingte Syntax zeigt die Zeichenfolge ‚wahr‘ in irb ohne puts
In welchem Zustand ist meine Ruby-Bedingung?
irb(main):001:0> if true
irb(main):002:1> 'is true'
irb(main):003:1> else
irb(main):004:1* 'is false'
irb(main):005:1> end
=> "is true"
mit ... aber wenn ich die gleiche Syntax in einem Skript aufrufen und es von der Kommandozeile ausführen, wird sie ignoriert. Warum?
# Odd behaviour:
puts "Why do only two of the three conditionals print?"
# This doesn't put anything to screen:
if true
'is true_1'
else
'is false'
end
puts "Seriously, why? Or better yet: how?"
# But this does:
if true
puts 'is true_2'
else
puts 'is false'
end
# And this works without "puts":
def truthiness
if 1.send(:==, 1)
'is true_3'
else
'is false'
end
end
puts truthiness
puts "Weird."
Als ich dies als ein Skript ausführen, zeigt es:
"Why do only two of the three conditionals print?
Seriously, why? Or better yet: how?
is true_2
is true_3
Weird."
FWIW, verfolge ich zusammen mit Sandi Metz Vortrag "Nichts ist Something" https://youtu.be/zc9OvLzS9mU ... und dazu hören : https://youtu.be/AULOC--qUOI Entschuldigung, wie ich Ruby neu bin und versuche, meinen Kopf einzuwickeln, wie es tut, was es tut.
EDIT: Nützliche Quellen:
http://ruby-doc.org/core-2.3.1/Kernel.html#method-i-puts
https://softwareengineering.stackexchange.com/questions/150824/is-the-puts-function-of-ruby-a-method-of-an-object
Interessant ... muss mit beginnen vertraut, sieht sehr nützlich. Wenn ich das explizite 'puts' in der irb - Bedingung verwende, wird das 'ist wahr' zusammen mit einer anderen Zeile von '=> nil' ausgedruckt. –
Ah ja, tut mir leid, du siehst den Rückgabewert der setzt die Operation, die Null ist. Ich habe meine Antwort entsprechend bearbeitet. – Ginty
Das liegt daran, dass ['Kernel # puts' ''nil' zurückgibt, wie in der Dokumentation angegeben] (http://ruby-doc.org/core/Kernel.html#method-i-puts). –