Ruby如何处理未声明的变量而不是php?

In php, I can comfortably write:

if (x > 100) 
{ 
   method(); 
}

knowing that if x doesn't exist, my program will treat it as a small bump in the road and keep going.

I'm used to php's ultra-lax variable typing and undeclared handling, and wondering what the rules of Ruby are in relation to this.

What is Ruby's default action when you try to evaluate something that isn't declared?

And if I can pepper this in too... does null, zero, and false equal the same thing in Ruby? Would

if(!x)
{
  puts 'works'
}

puts?

I know these are very simple questions, but either they're too obvious for me to catch or I'm using the wrong search phrases.

  1. Ruby will complain if you use a variable you have not decalred.

  2. In ruby you usually don't enclose an if in { and }, you put an if and end it with end.

  3. false nil and 0 are different things. Your code will complain that x is not defined and will only puts if x is false or nil

  4. In ruby to check if a value is nil you use nil? like so if !x.nil?

Starting from last question: In ruby, zero is not treated as false, the only falsy values are: nil and obviously false. Everything else is a "true" value.

Attention: I will use instance variables because it's not really normal there's a change for a local variable not to have been instantiated.

So why this wont work

if @x > 100 
  method()
end

Because > in ruby is actually a method call so if @x is nil (undefined variable) it will raise a NoMethodError because nil does not have that method defined.

NoMethodError: undefined method `>' for nil:NilClass

So what you can do in your condition is to first ensure @x has a value like this:

if @x && @x > 100 
  method()
end