Integer Arithmetic
Lesson 4 of 48 in Coddy's Ruby for GDScript Developers course.
GDScript integer division truncates toward zero and its remainder can be negative; Ruby integer division floors and modulo follows the divisor’s sign. GDScript int is a fixed-width signed value, while Ruby Integer expands beyond machine width. A floating operand changes division behavior. Port the numeric contract explicitly, especially for negative values or totals that can grow beyond the source representation.
GDScript: complete program
extends SceneTree
func _init():
var n = -7
var d = 3
print(str(n / d) + ":" + str(n % d))
quit()Ruby: complete program
puts(-7 / 3)
puts(-7 % 3)
puts(7.0 / 2)Challenge
EasyInput is nonnegative minutes up to 100000. Return hours and remaining minutes separated by a colon, without zero padding.
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)
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Ruby for GDScript Developers
1Start with Ruby
Run Ruby FilesOutput and CommentsNames and ObjectsInteger ArithmeticTruth and Boolean LogicString OperationsSymbols and KeysRecap: Packing Slip2Flow and Methods
Conditional ValuesCase and RangesLoops and Early ExitMethods and ReturnsDefaults and KeywordsRecap: Sensor Run5Objects and State
Modules as NamespacesClasses and InstancesReaders and WritersValidated ConstructionRecap: Wallet8Lending Desk Project
Lending Desk: CatalogLending Desk: BorrowPractice on your own: Online Ruby compiler