Project

General

Profile

Actions

Bug #11659

closed

Strange behavior setting previously-undefined local variables with a statement modifier

Added by mwpastore (Mike Pastore) about 9 years ago. Updated over 5 years ago.

Status:
Rejected
Assignee:
-
Target version:
-
ruby -v:
ruby 2.2.3p173 (2015-08-18 revision 51636) [x86_64-linux]
[ruby-core:71348]

Description

Consider a previously-undefined local variable var1:

irb(main):001:0> if defined?(var1).nil?; var1 = 'default'; end; var1
=> "default"

Consider previously-undefined local variables var1 and var2:

irb(main):001:0> var2 = 'default' if defined?(var1).nil?; var2
=> "default"

Consider a previously-undefined local variable var3:

irb(main):001:0> var3 = 'default' if true; var3
=> "default"

Consider a previously-undefined local variable var4:

irb(main):001:0> var4 = 'default' if defined?(var4).nil?; var4
=> nil

Oops! Why is var4 nil? Logically, considering the prior examples, it should be 'default'. Or are we missing something?

http://stackoverflow.com/questions/33537059/unless-defined-is-not-working-in-my-code

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0