Codectionary / Developer documentation / Ruby

Create a project folder and Ruby file, run it in a terminal, understand its output, then change it safely.

Create a folder and open an editor

Make a folder named ruby-first-program somewhere you can find again, such as your Documents folder. It keeps this exercise separate from other files. Open that folder in a plain-text code editor. VS Code and RubyMine are options, but any editor that saves plain text works. You need a local Ruby installation to run the file; follow the guide for your operating system if ruby --version does not show a version.

Inside the folder, create a new file named hello.rb. The .rb extension helps your editor identify Ruby code. On Windows, check that the actual filename is hello.rb rather than hello.rb.txt if file extensions are hidden. You do not need a Rails project, a package or a build step for this program.

  1. Create a folder named ruby-first-program.
  2. Open that folder in a text or code editor.
  3. Create hello.rb inside the folder.

Write and save one line of Ruby

Type the line below into hello.rb and save the file. Ruby reads the file as source code. puts is a method that writes its argument to standard output and adds a newline. The quoted text is a String, a value made of characters. Quotation marks tell Ruby where that text starts and ends; without them, Ruby would try to interpret the words as code.

A Ruby statement commonly ends at a newline, so this line does not need a semicolon. Ruby does allow semicolons to separate statements on one line, but adding one after every beginner statement is unnecessary. The exclamation mark is part of the greeting, not special Ruby syntax here.

hello.rb — save this file

puts "Hello, Codectionary!"

Open a terminal and run the file

Open a terminal in the ruby-first-program folder. In VS Code, Terminal > New Terminal opens one in the workspace; in another editor, open your system terminal and navigate to the folder. Check that hello.rb is in your current folder before running the command below. On Windows PowerShell, dir lists files; on macOS or Linux, ls does.

The ruby command starts the installed Ruby runtime. The hello.rb argument tells it which file to read and execute. The greeting appears in that same terminal, normally on the next line. It does not appear in the browser or in the Codectionary Playground; that Playground can edit Ruby but does not execute it. A successful run should display exactly one line: Hello, Codectionary!

Terminal — from the folder containing hello.rb

ruby hello.rb

Expected terminal output

Hello, Codectionary!

Modify the program and predict its output

Change the greeting to Hello, Sam!, save hello.rb, and run ruby hello.rb again. Predict the new line before looking at the terminal. Ruby reads the file each time you run the command, so unsaved editor changes are not part of the next run. This edit-and-run loop is a practical way to learn what a line does.

Next, put the name in a variable. The assignment name = "Sam" stores a String under the name name. In a double-quoted String, #{name} inserts the variable’s value; this is string interpolation. The resulting output is still Hello, Sam!, but changing the name now requires editing one value rather than rewriting the entire greeting.

hello.rb — a variable and interpolation

name = "Sam"
puts "Hello, #{name}!"

Put the greeting in a method

A method gives a reusable operation a name. The example below defines greet with a parameter also called name. Calling greet("Sam") passes the String into the method. Its final expression builds and returns the greeting, and the outer puts prints that returned String. The output is Hello, Sam!. The end keyword closes the method definition.

This is more structure than a one-line greeting needs, but it shows how the same idea can be reused for different names in a larger program. Try another name and verify the output changes without changing the method body.

hello.rb — a small reusable method

def greet(name)
  "Hello, #{name}!"
end

puts greet("Sam")

Recognise common first-program errors

If the terminal says ruby is not recognised or command not found, Ruby is not installed or the terminal has not picked up its PATH entry. Reopen the terminal and follow your platform’s installation guide. If Ruby says it cannot load hello.rb, check the current folder and the exact filename, including the .rb extension.

A missing closing quotation mark produces a syntax error because Ruby cannot tell where the String ends. A misspelling such as put instead of puts can produce an undefined-method error when that line runs. Read the reported file and line, fix one issue, save, then rerun. If the old greeting still appears, save the file and confirm you are executing the same hello.rb that you edited.

What to learn next

You have written, saved and executed a Ruby program. Try changing the message, using a different variable, and calling greet twice with different names. IRB lets you test small expressions quickly; saved .rb files are better when you want to keep and rerun a program. After this guide, learn variables, collections, decisions and methods in that order.

Syntax

puts "Hello, Codectionary!"

Examples

The first saved Ruby file

Save as hello.rb and run ruby hello.rb. Expected output: Hello, Codectionary!

puts "Hello, Codectionary!"
  1. ruby hello.rb asks the installed Ruby runtime to execute the saved file.
  2. puts prints its String argument followed by a newline.
  3. The terminal displays one greeting; the editor only stores the source until you run it.

Change the greeting with a variable

Expected output: Hello, Sam!

name = "Sam"
puts "Hello, #{name}!"
  1. name = "Sam" assigns a String to a variable.
  2. Double-quoted Strings allow #{name} to insert that value.
  3. puts writes the resulting greeting to the terminal.

Reuse the greeting in a method

Expected output: Hello, Sam!

def greet(name)
  "Hello, #{name}!"
end

puts greet("Sam")
  1. def starts a named method with one parameter; end closes it.
  2. The String expression is the method’s return value.
  3. puts prints the result after greet("Sam") runs.

Best practices

  • Save hello.rb before each run.
  • Read both the error message and reported file location when a command fails.
  • Predict the output before changing or rerunning an example.

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