From: "duerst (Martin Dürst)" <duerst@...>
Date: 2012-03-25T16:44:33+09:00
Subject: [ruby-core:43648] [ruby-trunk - Feature #4965] The problem of "print line if line = DATA.gets"


Issue #4965 has been updated by duerst (Martin D��rst).


Just for completeness, at today's developers' meeting in Akihabara, there were several people who were supportive of this proposal. Arguments given were that humans read code top-down, but not necessarily left-to-right character-by-character, and that if we would want to claim that Ruby works strictly top-down and per-line-left-to-right, then postfix if (and friends) would be impossible in the first place.
----------------------------------------
Feature #4965: The problem of "print line if line = DATA.gets"
https://bugs.ruby-lang.org/issues/4965#change-25148

Author: yimutang (Joey Zhou)
Status: Assigned
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category: 
Target version: 


Right now, I find the following code will raise an exception:

print line if line = DATA.gets 
__END__
abcdef

# undefined local variable or method `line' for main:Object (NameError)

"print line while line = DATA.gets" also doesn't work, maybe "unless" "until" modifiers either. 

It seems that the Ruby interpreter is too impatient, once seeing unassigned variable "line", it croaks.

However, the expression after "if" or "while" modifier should be evaluated first. Logically, I think the code above should work properly, there's no need to write "line = nil" before it.


-- 
http://bugs.ruby-lang.org/