Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com
Answertopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions
Privacy Policy

  




 

 

Ruby Programming
Previous Page Home Next Page

Restrictions

Because of the way irb works, there is a minor incompatibility between it and the standard Ruby interpreter. The problem lies in the determination of local variables.

Normally, Ruby looks for an assignment statement to determine if something is a variable---if a name hasn't been assigned to, then Ruby assumes that name is a method call.

eval "a = 0"
a
produces:
prog.rb:2: undefined local variable or method `a'
for #<Object:0x401c2ce0> (NameError)

In this case, the assignment is there, but it's within a string, so Ruby doesn't take it into account.

irb, on the other hand, executes statements as they are entered.

  irb(main):001:0> eval "a = 0"
  0
  irb(main):002:0> a
  0

In irb, the assignment was executed before the second line was encountered, so ``a'' is correctly identified as a local variable.

If you need to match the Ruby behavior more closely, you can place these statements within a begin/end pair.

  irb(main):001:0> begin
  irb(main):002:1*   eval "a = 0"
  irb(main):003:1>   a
  irb(main):004:1> end
  NameError: undefined local variable or method `a'
  (irb):3:in `irb_binding'
Ruby Programming
Previous Page Home Next Page

 
 
  Published under the terms of the Open Publication License Design by Interspire