Menu
CoddyTech

Reduce and Predicates

Lesson 19 of 48 in Coddy's Ruby for GDScript Developers course.

GDScript any/all and Ruby any?/all? can short-circuit and return true for an empty universal traversal. Reductions need a meaningful starting identity, especially when input is empty. Ruby predicate results use Ruby truthiness, so an empty array returned by a predicate is truthy even though it is false in a GDScript condition. Keep predicate intent explicit.

GDScript: complete program

extends SceneTree

func _init():
    print(["oak", "elm"].any(func(word): return word.begins_with("e")))
    print([].all(func(value): return value))
    quit()

Ruby: complete program

puts ["oak", "elm"].any? { |w| w.start_with?("e") }
puts [].all?
challenge icon

Challenge

Easy

Input is integers. Return the product of positive integers only. If there are no positive integers, return 1.

Return text from solve(input) without printing. The locked driver prints the returned text with one final newline and checks required public APIs independently.

Your code goes in exercise.rb. main.rb holds the supplied input/output code and cannot be edited.

Try it yourself

require_relative 'exercise'
def check(value)
  raise 'Public API check failed' unless value
end

puts solve(STDIN.read)
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Ruby for GDScript Developers

Practice on your own: Online Ruby compiler