Note that there are some explanatory texts on larger screens.

plurals
  1. PORuby: what is the pitfall in this simple code excerpt that tests variable existence
    text
    copied!<p>I'm starting with Ruby, and while making some test samples, I've stumbled against an error in the code that I don't understand why it happens.</p> <p>The code pretends to tests if a variable finn is defined?() and if it is defined, then it increments it. If it isn't defined, then it will define it with value 0 (zero). As the code threw an error, I started to decompose it in small pieces and run it, to better trace where the error was comming from. The code was run in IRB irb 0.9.5(05/04/13), using ruby 1.9.1p378</p> <p>First I certify that the variable finn is not yet defined, and all is ok:</p> <pre><code>?&gt; finn NameError: undefined local variable or method `finn' for main:Object from (irb):134 from /home/paulo/.rvm/rubies/ruby-1.9.1-p378/bin/irb:15:in `&lt;main&gt;' &gt;&gt; </code></pre> <p>Then I certify that the following inline-condition executes as expected, and all is ok:</p> <pre><code>?&gt; ((defined?(finn)) ? (finn+1):(0)) =&gt; 0 </code></pre> <p>And now comes the code that throws the error: </p> <pre><code>?&gt; finn=((defined?(finn)) ? (finn+1):(0)) NoMethodError: undefined method `+' for nil:NilClass from (irb):143 from /home/paulo/.rvm/rubies/ruby-1.9.1-p378/bin/irb:15:in `&lt;main&gt;' </code></pre> <p>I was expecting that the code would not throw any error, and that after executing the variable finn would be defined with a first value of 0 (zero). But instead, the code thows the error, and finn get defined but with a value of nil.</p> <pre><code>&gt;&gt; finn =&gt; nil </code></pre> <p>Where might the error come from?!? Why does the inline-condition work alone, but not when used for the finn assignment?</p> <p>Any help apreciated :)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload