TIL: Ruby stuff II

Continuing adventures in The Ruby Programming Language

I see you

Ruby doesn’t like when you use variables or methods that aren’t defined:

1
2
3
4
5
Ruby ❯ irb
irb(main):001:0> a 
NameError: undefined local variable or method `a' for main:Object  
  from (irb):2  
  from /Users/clint/.rbenv/versions/1.9.3-dev/bin/irb:12:in `<main>'

Makes sense –

1
a
doesn’t exist. However, if the interpreter has at least seen
1
a
, things will be different:

1
2
3
4
5
6
7
Ruby ❯ irb
irb(main):003:0> if false
irb(main):004:1> a = true
irb(main):005:1> end
=> nil
irb(main):006:0> a
=> nil

Above,

1
a
is never assigned, but it’s still seen by the interpreter, so it still exists in some way.

@ctshryock

About

My name is Clint Shryock. I develop things in Go and Ruby. I live in central Missouri, where the weather is beautiful 4 months of the year.
+-----------------+
|                       |
|      (ノ^_^)ノ      |
|                       |
|   ☜(゚ヮ゚☜)    |
|                       |
|     ౿(ఠ_ఠఎ)    |
|                       |
|        ಠ_ಠ         |
x                      x
  xxx           xxx
       xx    xx
           xx