79535211

Date: 2025-03-26 01:30:28
Score: 0.5
Natty:
Report link

@sawa said: Of course these are objects, and have to belong to some class. I am asking why they cannot be instances of the Object class.

@sawa The Ruby interpreter evaluates conditional expressions as either truthy or falsey. Since everything in Ruby is an object, the evaluation of a conditional expression results in an object.

All objects in Ruby except false and nil evaluate as truthy. So, all other objects like true, [] (an empty array,) 0 (the Integer ordinal zero,) even an empty String object, etc., evaluate as truthy. This means that when the expression is a comparison, the resultant object of the evaluation will be either true, false or nil. The interpreter does not need to do anything more with the result in order to make a decision as to code flow (i.e., execute the if or the else clause, etc.)

But if instead true, false and nil were bare instances of Object, then this would mean that class Object would have to have true? and false? query methods and every object (since everything is a descendant of Object,) would need to hold a Boolean and Nilness state variables, which just creates overhead.

Now think about the interpreter when it comes to evaluating each conditional expression. Instead of the simple "is it nil or false, otherwise true" test, it would have to call a Boolean query method upon the result object of each expression. Method calls take time and this would just add slow down execution.

It also would force programmers to state the truthiness / falsehood of their classes when the define them or of instance objects when instantiated which I can imagine would be convoluted or some new syntax in the argument list.

Reasons:
  • Long answer (-1):
  • Has code block (-0.5):
  • User mentioned (1): @sawa
  • User mentioned (0): @sawa
  • Low reputation (1):
Posted by: Dan Rathbun