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.
Ruby will complain if you use a variable you have not decalred.
In ruby you usually don’t enclose an
ifin{and}, you put anifand end it withend.falseniland0are different things. Your code will complain thatxis not defined and will onlyputsifxisfalseornilIn ruby to check if a value is
nilyou usenil?like soif !x.nil?