HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to RubyVariablesData TypesOperatorsStringsArraysHash (Dictionary)Conditions (if / else)LoopsMethods (def)Classes
REFERENCE
📖 Overview
🖥️ Output & Input
puts / print / pString interpolation ( #{} )gets
📦 Variables
🔢 Numbers & Math
✍️ String
📋 Array
🗂️ Hash (Dictionary)
🔁 Condition & Loop
🧩 Method & Class
Ruby›REFERENCE›Output & Input
🖥️

Output & Input

Printing to the screen and embedding values into text.

CommandDescription
puts / print / pPrints to the screen.
String interpolation ( #{} )Embedding variables/expressions into double-quoted strings.
getsReads a line from the user.

puts / print / p

Try it yourself →

Prints to the screen.

Syntax

puts value

Description

puts moves to a new line after every value. print does not add a newline at the end. p prints the value in an 'inspectable' format (shows arr/strings with quotes) — useful for debugging.

Example

puts "Hello"
print "side "
print "by side\n"
p [1, 2, 3]

Expected output

Hello
side by side
[1, 2, 3]

When to use?

Showing results; debugging with p.

String interpolation ( #{} )

Try it yourself →

Embedding variables/expressions into double-quoted strings.

Syntax

"text #{expression}"

Description

Inside double quotes, you write a variable or expression between #{ }; it automatically converts to text. It doesn't work in single quotes.

Example

name = "Alex"
puts "Hello #{name}, 2 times 3 = #{2 * 3}"

Expected output

Hello Alex, 2 times 3 = 6

When to use?

Creating dynamic text.

Common mistakes

  • ⚠️ In single quotes #{name} is printed as is; use double quotes.

gets

Try it yourself →

Reads a line from the user.

Syntax

gets.chomp

Description

Gets a line of text from the keyboard. Add .chomp to remove the trailing newline.

Example

puts "Name: "
name = gets.chomp
puts "Hello #{name}"

Expected output

(depending on the entered name) Hello Alex

When to use?

Getting input in console programs.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code