From: "Hanmac (Hans Mackowiak) via ruby-core" Date: 2023-04-06T07:23:29+00:00 Subject: [ruby-core:113148] [Ruby master Bug#19583] Unary minus inconsistency when used with variables and literals Issue #19583 has been updated by Hanmac (Hans Mackowiak). `-2` is a literal `- 2` is a function call of `-@` and -@ doesn't have preference over function call, see this: ```ruby a = 2 v = -a.to_s p v #=> 2 ``` ---------------------------------------- Bug #19583: Unary minus inconsistency when used with variables and literals https://bugs.ruby-lang.org/issues/19583#change-102683 * Author: hurricup (Alexandr Evstigneev) * Status: Open * Priority: Normal * Backport: 2.7: UNKNOWN, 3.0: UNKNOWN, 3.1: UNKNOWN, 3.2: UNKNOWN ---------------------------------------- This feels a bit inconsistent and I could not find an explanation. This is fine and `-` has higher precedence than `.` ``` -2.upto 0 do |arg| puts arg end ``` But this is not working, won't even compile (requires parens): ``` var = 2 -var.upto 0 do |arg| puts arg end ``` I presume that in the first example there is no unary minus operation, just negative literal. Ok, according to this - my assumption is correct. The question is - why? ``` def some puts -2 a = 42 puts -a end puts RubyVM::InstructionSequence.of(method :some).disasm ``` -- https://bugs.ruby-lang.org/ ______________________________________________ ruby-core mailing list -- ruby-core@ml.ruby-lang.org To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org ruby-core info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-core.ml.ruby-lang.org/