Menu
CoddyTech

Names and Objects

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

GDScript supports dynamic variables and optional static annotations, with := inferring a fixed type. Ruby names can be rebound to objects of different kinds without static variable declarations. Arrays and dictionaries are shared through assignment in GDScript, as ordinary objects are in Ruby. Rebinding a local name is different from changing a shared object, and block-local scope rules are not identical.

GDScript: complete program

extends SceneTree

func _init():
	var amount = 6
	if true:
		var doubled = amount * 2
		print(doubled)
	print(amount)
	quit()

Ruby: complete program

label = "cedar"
old = label
label = label.length
puts label
puts old
challenge icon

Challenge

Easy

Input is a quantity and a unit price, separated by whitespace. Return their product minus a fixed discount of 3 as decimal text. Both inputs are integers in 0..100.

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