Why Does Ruby Have Trueclass and Falseclass Instead of a Single Boolean Class

Why does Ruby have TrueClass and FalseClass instead of a single Boolean class?

It seems that Matz himself answered this question on a mailing list message in 2004.

Short version of his answer: "right now it works ok, adding a Boolean doesn't give any advantage".

Personally I don't agree with that; the aforementioned "string parsing" is one example. Another one is that when you are applying different treatment to a variable depending on its type, (i.e. a yml parser) having a "Boolean" class is handy - it removes one "if". It also looks more correct, but that's a personal opinion.

Make FalseClass behave like TrueClass with meta programming

It is not possible. One way to think about it is that there is no method Object#truthiness? that could be redefined.

In Ruby MRI, the truthiness test is the RTEST macro that is hardwired to mean anything but Qfalse and Qnil, the two constants corresponding to false and nil. You would have to change this to redefine what is "truthy" or not.

Outputing value of TrueClass / FalseClass to integer or string/

You could create a file such as lib/boolean_ints.rb and monkey-patch a to_i method to TrueClass and FalseClass, as you suggested. Then, in the models or controllers in which you need to use the to_i method, you could just put

require 'boolean_ints'

at the top, and they'd be available for use.

Why is a Regexp object considered to be falsy in Ruby?

This isn’t a bug. What is happening is Ruby is rewriting the code so that

if /foo/
whatever
end

effectively becomes

if /foo/ =~ $_
whatever
end

If you are running this code in a normal script (and not using the -e option) then you should see a warning:

warning: regex literal in condition

This is probably somewhat confusing most of the time, which is why the warning is given, but can be useful for one lines using the -e option. For example you can print all lines matching a given regexp from a file with

$ ruby -ne 'print if /foo/' filename

(The default argument for print is $_ as well.)

How to check if a Ruby object is a Boolean

Simplest way I can think of:

# checking whether foo is a boolean
!!foo == foo

Mandatory one-to-one relationship in Rails

Speaking from a UI developer perspective, this is what I'd start with.

I suspect you will often do two things -

1) display the flight in a list and include the departure and arrival information in the list item
2) sort the list by time of departure and arrival

These will be easier/faster to do if the attributes are on the flight. You'll avoid the joins. Since the information is required, no need to 'has_one'. You can always move them later in a migration if you need to. Keep it simpler until the use cases come up.

I think it will also be easier for you to make a has_many :arrivals (which are of type Flight) on the airport by simply looking at the Flight's arrival airport id.

|| and && aren't methods on Object -- what are they?

Both | and || are operators. || is part of the language while | is implemented as a method by some classes (Array, FalseClass, Integer, NilClass and TrueClass) .

In programming languages, | is used in general as the bitwise OR operator. It combines the bits of its integer operands and produces a new integer value. When used with non-integer operands, some languages convert them to integer, others prohibit such usage.

|| is the logical OR operator. It combines two boolean values (true or false) and produces another boolean value. When its operands are not boolean values, they are converted to boolean by some languages. Ruby (and JavaScript and other languages) evaluate its first operand as boolean and the value of the expression is the value of its first operand if its boolean value is true or the value of its second operand if the logical value of its first one is false. The type of the resulting value is its original type, it is not converted to boolean.

Each language uses its own rules to decide what non-boolean values are converted to false (usually the number 0, the empty string '' and null or undefined); all the other values are converted to true. The only "false" values in Ruby are false (boolean) and nil (non-boolean); all the other values (including 0) are "true".

Because true || anything is true and false && anything is false, many programming languages including Ruby implement short-circuit evaluation for logical expressions.

Using short-circuit evaluation, a logical expression is evaluated from left to right, one operand at a time until the value of the expression can be computed without the need to compute the other operands. In the examples above, the value of anything doesn't change the value of the entire expression. Using short-circuit evaluation, the value of anything is not computed at all because it does not influence the value of the entire expression. Being anything a method call that takes considerable time to execute, the short-circuit evaluation avoids calling it and saves execution time.

As others already mentioned in comments to the question, implementing || as a method of some class is not possible. The value of its second operand must be evaluated in order to be passed as argument to the method and this breaks the short-circuiting behaviour.

The usual representation of the logical values in programming languages uses only one bit (and I guess Ruby does the same.) Results of | and || are the same for operands stored on one bit.

Ruby uses the | symbol to implement different flavors of the OR operation as follows:

  • bitwise OR for integers;
  • non-short-circuit logical OR for booleans and nil;
  • union for arrays.

An expression like:

x = false | a | b | c

ensures that all a, b and c expressions are evaluated (no short-circuit) and the value of x is the logical OR of the logical values of a, b and c.

If a, b and c are method calls, to achieve the same result using the logical OR operator (||) the code needs to look like this:

aa = a
bb = b
cc = c
x = aa || bb || cc

This way each method is called no matter what values are returned by the methods called before it.

For TrueClass, FalseClass and NilClass, the | operator is useful when short-circuit evaluation is not desired.

Also, for Array (an array is just an ordered set), the | operator implements union, an operation that is the semantically equivalent of logical OR for sets.

Rails (or Ruby): Yes/No instead of True/False

No such built-in helper exists, but it's trivially easy to implement:

class TrueClass
def yesno
"Yes"
end
end

class FalseClass
def yesno
"No"
end
end

Why class_eval evaluates the string and block differently?

In the first case, you stuff a string into class_eval, and this class_eval is invoked on the class A. Hence, when the expression is evaluated, Module.nesting - which needs produce its nesting levels - finds itself inside an A, which in turn is evaluated inside a C.

In the second case, you pass a block, which is similar to a proc object. The effect is comparable to have a

 class C
p = Proc.new { print Module.nesting }
do_something(p)
end

The Proc represents a closure, i.e. the context is that of creating the Proc. It is clear, that the nesting here is only C, and this does not change, if you evaluate p inside do_something.

This is a good thing. Imagine the following situation:

def f(p)
x = 'f'
p.call
end

def g
x = 'g'
p = Proc.new { puts x }
f(p)
end

Because the binding for p occurs inside method g, the x referenced in the block refers to the local value x inside g, although f has a local variable of the same name. Hence, g is printed here. In the same way, the nesting at the point of block definition is reproduced in your example.



Related Topics



Leave a reply



Submit