Codectionary / Developer documentation / Ruby

Follow a Ruby source file from the terminal to its output, and understand the runtime, objects, errors, dynamic typing and garbage collection.

Start with a Ruby source file

A .rb file is an ordinary text file containing Ruby source code. It can hold expressions, method definitions, classes and comments. The extension helps tools recognise the file; it is not a compiled executable. You can write a small script in a text editor and save it without creating a Rails application or a project file.

After installing Ruby locally, open a terminal in the folder containing hello.rb and run ruby hello.rb. The command asks the selected Ruby implementation to execute that file. Ruby prints output from calls such as puts to the terminal. If the terminal says ruby is not recognised, the runtime is missing or the shell cannot find it; check the installation before changing the program.

Terminal — run a saved Ruby file

ruby hello.rb

Interpreter and runtime: what they do

An interpreter is software that takes a program written in a language and carries out its instructions. For a beginner, the useful idea is that the ruby command starts an implementation, which reads your source, checks that it can understand the syntax, and runs the program. “Interpreted” does not mean that every implementation must execute one source line at a time; implementations can prepare or optimise code internally.

The runtime is the environment that makes execution possible. It manages Ruby objects and method calls and provides access to built-in features and libraries. Your program may also interact with the operating system through file and network APIs when permitted. Rails is not part of the definition of the Ruby runtime: a plain Ruby script can run without Rails.

What happens when you run a file

First, the Ruby implementation reads the file and parses its syntax. If a quote or end keyword is missing, it reports a syntax error rather than running that invalid program as intended. When the source is valid, it evaluates expressions, calls methods and creates objects as the program requires. A puts call sends text to standard output, which a terminal normally displays.

This is a conceptual path, not a promise about internal instructions. CRuby may use internal instruction sequences and other implementation techniques; another Ruby implementation can use a different execution strategy. You only need the source → runtime → result model to write and debug your first scripts.

  1. Save Ruby source in a .rb file.
  2. Run the file with the ruby command.
  3. The selected implementation reads and checks the source.
  4. The runtime evaluates the program and calls methods.
  5. Inspect output or an error in the terminal.

Objects and dynamic typing

In Ruby, values such as 7, "hello" and [1, 2] are objects. An object belongs to a class and responds to methods. For example, a String responds to upcase, which returns uppercase text. The class method lets you inspect an object’s class. You can also define your own classes when a program needs its own kinds of objects.

A variable name refers to an object; you do not declare a permanent type for that name. You can assign text and later assign a number to the same name. That is dynamic typing. Operations still have rules: asking an Integer to use a String-only method can raise an error at runtime. The flexibility is useful, but testing the paths your program may take is important.

How Ruby reports errors

A syntax error means Ruby could not understand the source as written; look near the reported location for a missing quote, bracket or end. An exception happens during execution—for example, a method is called on an object that does not provide it. An unhandled exception normally displays its class, a message and a backtrace that identifies relevant file and line locations. Exact formatting can vary by Ruby version and implementation.

Read the first useful error location and the exception message, then inspect the values involved. Do not hide every error with a broad rescue clause: it can make a faulty program look successful. Ruby lets you rescue exceptions you can meaningfully handle, while unexpected ones should remain visible during learning and debugging.

Who manages memory?

Programs create objects as they run. Ruby implementations manage memory for Ruby objects with garbage collection: when an object is no longer reachable by the running program, it may eventually be reclaimed so that memory can be reused. You do not normally free each ordinary Ruby object manually.

Garbage collection is not a schedule you should rely on. It does not guarantee that an open file or network connection closes at a particular moment; use the appropriate APIs to close resources. Implementations can use different collection strategies, so this guide describes the shared concept rather than CRuby’s internal algorithm.

Ruby has more than one implementation

CRuby, also called MRI (Matz’s Ruby Interpreter), is the reference implementation and the one many beginners install. JRuby runs on the Java Virtual Machine; other implementations serve different environments and goals. They aim to run Ruby programs, but their internals, supported integrations and performance characteristics can differ.

If a guide discusses a CRuby-specific optimisation, extension or error display, do not assume every Ruby runtime behaves identically. For your first programs, use one supported installation and the documentation appropriate to its version. Learn about alternative implementations when a project gives you a reason to use one.

Try the execution cycle

Save the example below as hello.rb, run it locally, then change the name and run the saved file again. Expect two lines: HELLO, SAM and String. The first line comes from calling upcase on a String; the second prints that object’s class. If nothing changes, confirm you saved the file and ran it from the correct folder.

Codectionary’s Playground can edit and highlight Ruby source but cannot currently execute Ruby. To see Ruby output, use a local Ruby installation or an official Ruby learning environment. When this example makes sense, continue with variables, methods and error handling.

Syntax

ruby hello.rb

Examples

Run a Ruby file and inspect an object

Save as hello.rb and run locally. Expected output: HELLO, SAM and String on separate lines.

message = "Hello, Sam"
puts message.upcase
puts message.class
  1. The first line assigns a String object to the name message.
  2. upcase returns an uppercase String; puts prints it as HELLO, SAM.
  3. class reports the object’s class, and the final puts prints String.
  4. Change the message, save the file and run it again to connect the source with the terminal output.

Best practices

  • Save the file before running ruby filename.rb, and check that the terminal is in the correct folder.
  • Read the exception class, message and file location before editing unrelated code.
  • Close files and other resources explicitly through suitable APIs; do not depend on garbage-collection timing.

Runtime & versions

Ruby interpreter. Feature availability depends on the language and runtime version used by your project; consult the official reference below.

Ruby documentation

Specifications & further reading