2016-05-17 5 views
-1

Ich möchte eine Neg-Zahl in Ruby in eine Position umwandeln, also benutze die ABS-Methode.ruby ​​apply-Methode zur String-Interpolation

puts("You are #{playerGuess - num} away") 

Dies ist der aktuelle Code aber möchte das Ergebnis von PlayerGuess - num immer positiv machen. Wie wendet man die ABS-Methode an? wenn ich #{playerGuess - num} tun gibt es nur das Wort abs

+0

Wenn 'playerGuess' und' num' sind beide 'Fixnum' ist, die doc für' abs' ist hier: [NumeriC#abs] (http://ruby-doc.org/core-2.0/Numeric.html#method-i-abs). Wenn einer oder beide "Float" sind (also ist ihr Unterschied ein 'Float'), ist es hier: [Float # abs] (http://ruby-doc.org/core-2.2.0/Float.html # Methode-i-abs). [NumeriC# abs] (http://ruby-doc.org/core-2.0/Numeric.html#method-i-abs) deckt beide ab. –

Antwort

Verwandte Themen