NAME
Sidef::Types::Bool::Bool
DESCRIPTION
This class implements the Boolean type in Sidef, representing logical true and false values. Boolean objects are immutable and are the result of comparison operations, logical operations, and conditional expressions. The two Boolean values are true and false.
SYNOPSIS
var obj = Bool(1) # true
var obj = Bool(0) # false
var obj = Bool("string") # true
var obj = Bool(nil) # false
# Boolean operations
true & false # false (logical AND)
true | false # true (logical OR)
true ^ false # true (logical XOR)
!true # false (logical NOT)
# Comparisons return Bool
5 > 3 # true
"hello" == "world" # false
# Conditionals use Bool
if (true) {
say "This executes"
}
INHERITS
Inherits methods from:
* Sidef::Object::Object
METHODS
&
a & b
Returns the logical AND of two boolean values. Returns true only if both operands are true, otherwise returns false.
true & true # true
true & false # false
false & false # false
^
a ^ b
Returns the logical XOR (exclusive OR) of two boolean values. Returns true if exactly one operand is true, otherwise returns false.
true ^ false # true
true ^ true # false
false ^ false # false
|
a | b
Returns the logical OR of two boolean values. Returns true if at least one operand is true, otherwise returns false.
true | false # true
false | false # false
true | true # true
false
self.false
Returns the boolean value false. This is a class method that returns the false singleton object.
Bool.false # false
true.false # false (class method callable on instances)
is_true
self.is_true
Returns true if the boolean value is true, false otherwise. This method tests the truthiness of the boolean object.
true.is_true # true
false.is_true # false
neg
self.neg
Returns the logical negation of the boolean value. If the value is true, returns false; if the value is false, returns true.
true.neg # false
false.neg # true
!true # false (using operator form)
Aliases: not, flip, toggle, is_false
new
self.new
Bool.new(value)
Creates a new Boolean object from the given value. Any value can be converted to a boolean: - false, 0, empty string, nil become false - Everything else becomes true
Bool.new(1) # true
Bool.new(0) # false
Bool.new("text") # true
Bool.new("") # false
Bool.new(nil) # false
Bool.new([]) # true (empty array is truthy)
Aliases: call
pick
self.pick
Returns a random boolean value (either true or false) with equal probability.
Bool.pick # randomly returns true or false
true.pick # randomly returns true or false
This is useful for randomized algorithms and simulations.
Aliases: rand
run
self.run
Returns back the self object. This method simply returns the boolean value unchanged, useful for consistency with other types' interfaces.
true.run # true
false.run # false
to_b
self.to_b
Returns the boolean value itself. Converts the object to a boolean (which for a Bool object is a no-op, returning self).
true.to_b # true
false.to_b # false
Aliases: to_bool
to_s
self.to_s
Returns the string representation of the boolean value. Returns the string "true" for true values and "false" for false values.
true.to_s # "true"
false.to_s # "false"
say true # prints: true
Aliases: dump, to_str
true
self.true
Returns the boolean value true. This is a class method that returns the true singleton object.
Bool.true # true
false.true # true (class method callable on instances)
OPERATORS
The Bool class supports the following operators:
&- Logical AND|- Logical OR^- Logical XOR!- Logical NOT (negation)&&- Short-circuit AND||- Short-circuit OR
TRUTHINESS
In Sidef, the following values are considered false:
false- the boolean false value0- numeric zero""- empty stringnil- the null/undefined value
All other values are considered true, including:
true- the boolean true valueAny non-zero number
Any non-empty string (including
"0")Any array or hash (even if empty)
Any object
EXAMPLES
# Using booleans in conditionals
var is_valid = true
if (is_valid) {
say "Valid!"
}
# Boolean algebra
var a = true
var b = false
say (a & b) # false
say (a | b) # true
say (a ^ b) # true
say !a # false
# Comparison operations return Bool
var result = (10 > 5)
say result.class # Bool
say result # true
# Converting to boolean
say Bool(42) # true
say Bool(0) # false
say Bool(nil) # false
say Bool([]) # true
# Random boolean
var coin_flip = Bool.pick
say (coin_flip ? "Heads" : "Tails")