Return True Only If All Values Evaluate to True in Ruby

Return true only if all values evaluate to true in Ruby

You can use the all? function from the Enumerable mix-in.

elements = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
return elements.all? { |elem| elem % 2 != 0 }

Or, as pointed out in the comments, you could also use odd? if you're looking specificially for odd values.

elements = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
return elements.all?(&:odd?)

Ruby: why does =~ evaluate to true in an if statement?

The predicate after if will be treated as a boolean value.

In Ruby, only false and nil are treated as false. Anything other will be evaluated to true, so 0, [], {} are all true in boolean context.

From Ruby doc:

nil and false are both false values. nil is sometimes used to indicate "no value" or "unknown" but evaluates to false in conditional expressions.

true is a true value. All objects except nil and false evaluate to a true value in conditional expressions.

And you may have a look at the control expressions.

Write a function to return true or false if passed 1/0 or true/false

Some ideas:

def boolean(x)
%w{1 true}.include?(x).to_s
end

def boolean(x)
(x == '1' || x == 'true').to_s
end

There's also the wannabe bool gem:

require 'wannabe_bool'

'1'.to_b # => true
'0'.to_b # => false

'true'.to_b # => true
'false'.to_b # => false

Trying to use .reduce to return true if there are truthy values else false if there are falsey values

You need to understand the difference between "truthiness"/"falsiness" and being true/false.

Ruby would evaluate everything that is not nil or false as truthy, so you can write:

puts 1 if 1

and it will puts 1.

In this assignment you're asked to use truthiness, so instead of comparing to true you can use ruby to do the conversion for you using double negation !!:

pry> [1, "2", nil, true, false].map{|e| !!e}
=> [true, true, false, true, false]

Also note that a block in reduce accepts two params - one is the current element and another one is accumulator - the result of interating over previous elements. Use both in the block, otherwise you'll be returning result depending only on the last element of the array.

Why does .all? return true on an empty array?

In Ruby you can never loop over an empty collection (array, hashes, etc.), so in your case your block never gets executed. And if the block never gets executed, all? returns true (there is no condition to make the result false).

Read about all? in the Ruby documentation.

You can simply achieve your goal by

  !array.empty? && array.all? { |value| value == 2 }

Method over Boolean array should return true if any value in the array is true

Your code there is testing bool & bool & bool. Boolean#& is equivalent to && in general usage. As you might suspect, the corollary here is Boolean#|, which is equivalent to || in general usage. So, in your code, you would use test.reduce(:|) to decide if any boolean in the list is true.

That said, Cary is correct in that Ruby already has facilities for checking the truthiness of any value in an enumerable, via Enumerable#any? (and can check the truthiness of all values via Enumerable#all?), so you should just use those rather than rolling your own checker.

Write a function to return true or false if passed 1/0 or true/false

Some ideas:

def boolean(x)
%w{1 true}.include?(x).to_s
end

def boolean(x)
(x == '1' || x == 'true').to_s
end

There's also the wannabe bool gem:

require 'wannabe_bool'

'1'.to_b # => true
'0'.to_b # => false

'true'.to_b # => true
'false'.to_b # => false

How can I do a strict check on if a Ruby array contains only certain values?

Simple subtraction will provide you desired output,

def compare(checked_array, standard)
(checked_array - standard).empty?
end

Why does truth && string return string

Boolean operators in ruby are short-circuiting: if it is possible to determine the value of the expression from the left-hand argument, the right-hand argument isn't evaluated.

Therefore, a simpler mental model for evaluation of a boolean expression involving && is to consider first expressions involving only two operands: the left-hand operand is evaluated first; if the value of this operand is nil or false, the operand is returned and the right-hand operand isn't evaluated; if the left-hand operand is anything else, the right-hand operator is evaluated and its value is returned.

From this definition, it's clear that, as you note, expressions involving boolean operators don't return true or false, but simply a true value or a false value. It's worth noting that this doesn't make any difference in a context where a boolean expression is used only for its true-ness or false-ness.

Being the boolean operators left-associative, it's easy to determine the order of evaluation of an expression containing more than one operator, remembering that && has higher precedence than || (be careful however that and and or have the same precedence). Having done this, we can easily see that the value of the expression is the last evaluated element, i.e. the element that permits to determine the overall true-ness or false-ness of the expression.

In your examples (an expression composed only by && operators) the value of the expression is known as soon as the first false value is encountered, or after the last element has been evaluated as a true value; so, the last element evaluated will be the last element if all the elements preceding it are true valued, and the first false valued element if any is encountered.

You might wonder why the value of the expression isn't converted to true or false; actually, this kind of behavior can be used in idioms like

x = x || default

or the more brief

x ||= default

that is used to check if x is nil and, in that case, assign a default value to it.



Related Topics



Leave a reply



Submit