This question is in response to master412160's question:
I was considering responding via profile or private message, but I think this is a topic that would interest any programmer.
Foreward
This post is going to cover more in depth Ruby's object oriented programming style by explaining the nil class. Before I continue, notice I say nill class. That is correct, an object in Ruby can be defined in a class that stores inexistent objects.
Nil verse Null
PHP Defines Null as:
Explanations by Comparisons
In languages such as C or Java, programmers must ensure their script is not calling upon the inexistent value NULL. If null is fired in a script, a "NullPointerException" will be thrown and can lead the to unexpected results.
Java testing for null:
PHP testing for null:
Notice in both of these languages, NULL is a static value and will always return true or false. So what?
Well, let's jump in to the Ruby console and do some tests with the nil class that in other languages would be considered NULL.
Whenever you see ">" that is human input into the console.
Whenever you see "=>" that is a console message via the human's input.
$ rails console
> defined? the_object
=> nil
=> 1
> defined? the_object
=> "local-variable"
=> true
Interestingly enough, in Ruby, 'nil' is a singleton object of the nil class. Therefore 'nil?' is actually a method to evaluate the singleton objects of the nil class which in this case was "the_object". It will never raise a NoMethodError as in other static class programming languages where null must always equal null. nil can equal true, false, or self (the singleton method of the nil class) In Ruby, it goes like this: Object.class.superclass.superclass.superclass
Object = Object
Object.class = Class
Object.class.superclass = Module
Object.class.superclass.superclass = Object
Object.class.superclass.superclass.superclass = BasicObject
(Maybe I will explain that another time)
While it is hard to understand not knowing Ruby, once you start writing codes, you realize the power of this class.
And with Ruby anything is possible and more then with php?
I was considering responding via profile or private message, but I think this is a topic that would interest any programmer.
Foreward
This post is going to cover more in depth Ruby's object oriented programming style by explaining the nil class. Before I continue, notice I say nill class. That is correct, an object in Ruby can be defined in a class that stores inexistent objects.
Nil verse Null
PHP Defines Null as:
There is only one value of type null, and that is the case-insensitive constant NULL.
- Null is Ruby's nil
- nil is the class of the singleton object nil
- nil? implies an object
Explanations by Comparisons
In languages such as C or Java, programmers must ensure their script is not calling upon the inexistent value NULL. If null is fired in a script, a "NullPointerException" will be thrown and can lead the to unexpected results.
Java testing for null:
Code:
if (theObject != null) {
theObject.displayInfo();
} else {
System.out.println("This object does not exist")
}
PHP testing for null:
Code:
<?php
error_reporting(E_ALL);
$theObject = NULL;
var_dump(is_null($inexistent), is_null($theObject));
?>
Notice in both of these languages, NULL is a static value and will always return true or false. So what?
Well, let's jump in to the Ruby console and do some tests with the nil class that in other languages would be considered NULL.
Whenever you see ">" that is human input into the console.
Whenever you see "=>" that is a console message via the human's input.
$ rails console
> defined? the_object
=> nil
- '?' means you call "the_object" upon itself, therefore assuming the possibility that it may or may not exist.
- the console responds by telling us "the_object" does not exist.
=> 1
- you command the console to assign "the_object" a value of "1"
- the console confirms 1
> defined? the_object
=> "local-variable"
- you now command the console to assign "the_object" to 'nil', which "should" insinuate a reassignment to "the_object" which was previously the integer "1"
you call the object class upon itself once again, which holds two contradictory values and asking if defined - the console respond that it is indeed defined. But haven't we just commanded it be assigned to 'nil'?
=> true
- so does "the_object" exist or not?
- amazingly the console responds that the object is nil, but this does not mean that object is 'null'. This can be verified by the "defined?" method returning "local-variable"
Interestingly enough, in Ruby, 'nil' is a singleton object of the nil class. Therefore 'nil?' is actually a method to evaluate the singleton objects of the nil class which in this case was "the_object". It will never raise a NoMethodError as in other static class programming languages where null must always equal null. nil can equal true, false, or self (the singleton method of the nil class) In Ruby, it goes like this: Object.class.superclass.superclass.superclass
Object = Object
Object.class = Class
Object.class.superclass = Module
Object.class.superclass.superclass = Object
Object.class.superclass.superclass.superclass = BasicObject
(Maybe I will explain that another time)
While it is hard to understand not knowing Ruby, once you start writing codes, you realize the power of this class.







