Project

General

Profile

Actions

Feature #1141

closed

assignment of variable in "right" if statement fails

Added by hagus (Luke Burton) over 15 years ago. Updated over 13 years ago.

Status:
Rejected
Assignee:
-
Target version:
-
[ruby-core:21967]

Description

=begin
In Ruby 1.8

p(x) if x=42
(irb):1: warning: found = in conditional, should be ==
NameError: undefined local variable or method `x' for main:Object
from (irb):1

In Ruby 1.9

$ irb19
irb(main):001:0> p (x) if (x=42)
NameError: undefined local variable or method x' for main:Object from (irb):1 from /usr/local/bin/irb19:12:in '

However, if you move the if statement to the line before, naturally everything works.

Should variable assignment in "right" if clauses be permitted? I think so, since it allows you to be fairly concise and not have to do:

if x = 42
p(x)
end

"Right" if clauses are permitted because they read naturally, I assume. I'm not sure whether people regard variable assignment in this case as reading very "naturally", but there is at the very least a lack of consistency between this type of if clause, and the more regular type of if clause.
=end


Related issues 4 (0 open4 closed)

Related to Ruby master - Bug #13543: local variable declarationRejectedActions
Has duplicate Ruby master - Feature #4965: The problem of "print line if line = DATA.gets"Rejectedmatz (Yukihiro Matsumoto)07/03/2011Actions
Has duplicate Ruby master - Feature #6224: Make variables in if/unless conditions available to conditional statementsRejectedmatz (Yukihiro Matsumoto)03/29/2012Actions
Has duplicate Ruby master - Feature #8600: Recognize variables declared in if modifier at end of lineRejected07/04/2013Actions
Actions #1

Updated by matz (Yukihiro Matsumoto) over 15 years ago

  • Status changed from Open to Rejected

=begin
local variable scope determined up to down, left to right. So a local variable first assigned in the condition of if modifier is not effective in the left side if body. It's a spec.

=end

Actions #2

Updated by duerst (Martin Dürst) over 7 years ago

  • Related to Bug #13543: local variable declaration added
Actions

Also available in: Atom PDF

Like0
Like0Like0