5

I am confused with the way Ruby keeps track of variables. For example:

case 1:

    if true
       a
    end

will give you an error saying undefined local variable or method a.

case 2:

    if false
      a
    end
    a

will give you same the error for the second a, not for the first a.

case 3:

    if false
      a=2
    end
    a  #=> nil
    defined? a  #=> 'local-variable'

If you compare case 2 and case 3, in case 2 it ignored the error first a. I think its because of ruby's execution path has not reached the variable a due to false in condition. Same thing when I do with assignment in case 3. It gives me variable a defined but with nil value. Can someone explain the way it works?

sawa
  • 165,429
  • 45
  • 277
  • 381
Rahul Tapali
  • 9,887
  • 7
  • 31
  • 44

1 Answers1

3

In parse time if Ruby found any assignment such a=2,then local variable is created at that moment.It does not matter if you put in inside of any false conditional expression or not. Otherwise a legitimate error will be thrown as undefined local variable or method a,if you try to use the variable such as a here,before it's creation with the assignment(=) operator.

Look Confusion with the assignment operation inside the fallacy if block

Community
  • 1
  • 1
Arup Rakshit
  • 116,827
  • 30
  • 260
  • 317
  • Then it should throw error for case 2 (for first `a`) – Rahul Tapali Jul 23 '13 at 09:35
  • @codeit what i said local variable creation is done at compile time,not runtime. – Arup Rakshit Jul 23 '13 at 09:38
  • Is ruby interpreter language or compiled language? If every ruby program compiles then case 2 must give error. If it is purely interpreted then it should give error for case 3. If it is combination of both then explain at what time it compiles. – Rahul Tapali Jul 23 '13 at 09:44
  • 4
    Why would it raise an error? That code is never executed, therefore it cannot possibly raise an error. – Jörg W Mittag Jul 23 '13 at 09:45
  • 5
    There is no such thing as an interpreted language or a compiled language. Interpretation and compilation are a trait of, well, the interpreter and compiler, not the language. A language isn't compiled or interpreted, a language just *is*. Ruby has many different implementations: MRI is a pure interpreter. YARV is two-stage implementation, first it compiles Ruby to YARV bytecode, then it interprets that bytecode. Rubinius is a two-stage implementation where the second stage is in turn a mixed-mode implementation: first it compiles Ruby to Rubinius bytecode, then that bytecode gets either … – Jörg W Mittag Jul 23 '13 at 09:48
  • 4
    … interpreted or compiled to native code, depending on which one will give better performance. JRuby either interprets Ruby or it compiles it to JVM bytecode. IronRuby either interprets Ruby or it compiles it to CIL bytecode. MacRuby always compiles to native code (I think, they might have changed it since I last looked.) – Jörg W Mittag Jul 23 '13 at 09:50
  • @JörgWMittag you said `There is no such thing as an interpreted language or a compiled language.` That depends on view of person. Some people think as languages are of different types (i.e.) `Interpreted` like ruby, python and `Compiled` like C, C++ . Other people think as you do. – Rahul Tapali Jul 23 '13 at 10:13
  • 4
    Then those people are wrong. All existing implementations of Python have a compiler. Why would Python be an interpreted language? There are interpreters for C. Why would C be a compiled language? People who talk about compiled languages don't understand the difference between a language and an implementation of a language, or more generally, they don't understand the difference between an abstraction and a concrete implementation of that abstraction. Put another way: they simply don't understand abstraction. – Jörg W Mittag Jul 23 '13 at 10:37