Hello World (first programs) · Beginner · 14 min
Hello World in Ruby: irb, puts and a first gem with Bundler
By Padding · Updated 18 Nov 2026
Ruby 4.0.10 · ruby:4.0
You will install Ruby 4.0, see how puts, print and p differ in irb, run a script that greets the user with a block, and print a table with your first gem managed by Bundler.
On this page
Ruby is an interpreted language built around one idea taken further than most: everything is an object. The number 5, the text "hello" and even nil, Ruby's word for "nothing", all have methods you can call. That is why a Ruby loop can be written as 5.times, asking the number five to do something five times.
In this tutorial you install Ruby 4.0, try its three printing methods in irb (Interactive Ruby) and see why they behave differently, save and run a script, read a name from the keyboard and repeat a greeting with a block. Then you go one step further: you create a Gemfile with Bundler, add a small library (a gem) and use it.
Ruby is best known for Ruby on Rails, the web framework behind GitHub, Shopify and many smaller products. It is also widely used for scripts and command-line tools: Homebrew, the macOS and Linux package manager, is written in Ruby, and every Homebrew formula is a small Ruby file. A new Ruby version comes out every year on 25 December; Ruby 4.0 arrived on Christmas Day 2025, thirty years after the language was first released.
Step 1 · Install Ruby 4.0
Ubuntu's own ruby package lags behind the current release, so install Ruby with a version manager. A version manager installs Ruby inside your home folder and lets each project pick its version. This tutorial uses mise, which downloads a ready-built Ruby on Linux and Apple Silicon Macs instead of compiling it.
- Ubuntu or macOS: install mise, then install Ruby 4.0 as your default:
curl https://mise.run | sh
echo 'eval "$(~/.local/bin/mise activate bash)"' >> ~/.bashrc
source ~/.bashrc
mise use --global ruby@4.0On macOS your shell is zsh, so use these two lines in place of the second and third lines above:
echo 'eval "$(~/.local/bin/mise activate zsh)"' >> ~/.zshrc
source ~/.zshrc- Windows: download the newest Ruby 4.0 installer "with Devkit" from rubyinstaller.org and run it, leaving "Add Ruby executables to your PATH" ticked. Open a new terminal afterwards so it sees the change.
Now check the version:
ruby --versionCheck it worked
The terminal prints a line starting ruby 4.0. (4.0.10 at the time of writing; the Windows installer can be one patch behind, so ruby 4.0.9 is fine too), followed by a date and your platform, such as x86_64-linux. If it prints ruby 3.x or 2.6, you are seeing an older Ruby that came with your system: open a new terminal so mise's activation line runs, then try again. command not found means the folder holding ruby is not on your PATH, the list of folders your terminal searches for programs.
Step 2 · Compare puts, print and p in irb
Start Interactive Ruby:
irbThe prompt changes to something like irb(main):001>. irb runs each line as soon as you press Enter and shows the line's value after =>. Type these lines one at a time:
irb(main):001> puts "Hello, world"
Hello, world
=> nil
irb(main):002> print "Hello, world"
Hello, world=> nil
irb(main):003> p "Hello, world"
"Hello, world"
=> "Hello, world"All three print, but differently:
puts("put string") prints the text and then a new line. It returnsnil.printprints the text with no new line, which is why=> nillands on the same line.pprints the inspected form of a value: strings keep their quotes, so you can see exactly what you have. It also returns the value itself, which makes it the tool for debugging. When a value looks wrong,pit.
Notice there are no brackets around "Hello, world". In Ruby, brackets around a method's arguments are optional, so puts "Hello" and puts("Hello") are the same call. Now see that everything is an object:
irb(main):004> 5.class
=> Integer
irb(main):005> "hello".upcase
=> "HELLO"
irb(main):006> nil.class
=> NilClass
irb(main):007> p nil
nil
=> nil5.class asks the number 5 what kind of object it is. "hello".upcase calls the upcase method on a string. Even nil is an object of class NilClass. Leave irb with exit.
Check it worked
puts shows Hello, world with => nil on the next line, print shows Hello, world=> nil on one line, and p shows the text in quotes. 5.class shows Integer. After exit your normal terminal prompt is back.
Step 3 · Save hello.rb and run it
Make a folder for this tutorial and create hello.rb in it. On macOS or Linux you can paste the whole block into the terminal.
On Windows: the cat > NAME <<'RUBY' ... RUBY blocks in this tutorial are a bash shortcut for writing a file. Wherever you see one, save the lines between the two RUBY markers as NAME (here hello.rb) in your editor instead, then run any ruby command that follows. Make the folder with mkdir ~\hello-ruby-demo and move into it with cd ~\hello-ruby-demo in PowerShell.
mkdir -p ~/hello-ruby-demo
cd ~/hello-ruby-demo
cat > hello.rb <<'RUBY'
# My first Ruby script
puts "Hello, world"
RUBY
ruby hello.rbLine by line:
# My first Ruby scriptis a comment. Ruby ignores everything from#to the end of the line.puts "Hello, world"prints the text and a new line, as in irb. A script does not show=>values; only what you print appears.ruby hello.rbstarts the Ruby interpreter, which reads the file, checks the whole thing for syntax errors, then runs it from top to bottom.
Check it worked
The terminal prints Hello, world and returns to the prompt. ls in the folder shows hello.rb.
Step 4 · Greet the user and repeat with a block
Create greet.rb, which asks for a name and then uses a block:
cat > greet.rb <<'RUBY'
print "What is your name? "
name = $stdin.gets.chomp
puts "Hello, #{name}"
puts "Your name has #{name.grapheme_clusters.length} letters."
3.times do |i|
puts "#{i + 1}. Welcome to Ruby, #{name}"
end
RUBYLine by line:
print "What is your name? "shows the question without a new line, so the answer is typed on the same line.$stdin.getswaits for the user to type a line and press Enter, then returns it, including the Enter key as a trailing new line character ("Ada\n").$stdinis the keyboard input..chompremoves the trailing new line, giving"Ada". Calling one method on the result of another like this is called chaining.name = ...stores the result in a variable. Ruby variables need no type declaration."Hello, #{name}"is string interpolation: Ruby replaces#{...}with the value inside. This works only in double quotes.name.grapheme_clusters.lengthcounts the letters as a reader sees them. A name such as Adébáyọ̀ can contain combining tone marks, whichname.lengthcounts as separate characters (8 for Adébáyọ̀);grapheme_clustersgroups each letter with its marks.3.times do |i| ... endis a block.timesis a method on the number 3, and the code betweendoandendis a chunk of code handed to that method, which runs it three times.|i|names the value the method passes in on each run: 0, then 1, then 2.
Run it and type your name:
ruby greet.rbA block can also be written with braces on one line: 3.times { |i| puts i }. Ruby style uses braces for short one-line blocks and do ... end for blocks that span several lines. Blocks are everywhere in Ruby: you meet them again in the next step.
Check it worked
After you type Ada and press Enter, the script prints Hello, Ada, Your name has 3 letters. and three numbered lines ending 3. Welcome to Ruby, Ada. Typing Adébáyọ̀ reports 7 letters, the count a person reading the name would give.
Step 5 · Add your first gem with Bundler
A gem is a packaged Ruby library. Bundler, which comes with Ruby, records the gems a project needs in a file called Gemfile and the exact versions it installed in Gemfile.lock, so the project behaves the same on another computer. Create a Gemfile and add the terminal-table gem, which draws text tables:
cd ~/hello-ruby-demo
bundle init
echo 'gem "terminal-table", "~> 4.0"' >> Gemfile
bundle installbundle initwrites a starterGemfilethat names rubygems.org, the public gem server, as the source.- The
echoline appends one line to the Gemfile. On Windows, openGemfilein your editor and addgem "terminal-table", "~> 4.0"as a new last line instead: in Windows PowerShell 5.1,>>writes UTF-16 text into the UTF-8 file andbundle installthen fails to read it."~> 4.0"means "4.0 or any later 4.x version", so a future 5.0 with breaking changes is not picked up by accident. bundle installdownloads the gem and anything it depends on, then writesGemfile.lock. The download is small, which matters if you are on prepaid mobile data.
Now use the gem. Create table.rb:
cat > table.rb <<'RUBY'
require "bundler/setup"
require "terminal-table"
names = ["Ada", "Grace", "Adébáyọ̀"]
rows = names.map { |name| [name, name.grapheme_clusters.length] }
puts Terminal::Table.new(headings: ["Name", "Letters"], rows: rows)
RUBY
ruby table.rbrequire "bundler/setup"tells Ruby to load gems only at the versions inGemfile.lock. Without it you can runbundle exec ruby table.rbto the same effect.require "terminal-table"loads the gem's code.names.map { |name| ... }is another block:mapruns it once for each name and collects the results into a new list, here pairs of a name and its letter count.Terminal::Table.new(...)builds the table.headings:androws:are keyword arguments, named so the order does not matter.
Check it worked
bundle install ends with a line starting Bundle complete! and the folder now has Gemfile.lock. ruby table.rb prints something like this, with the column edges lined up even on the row with tone marks:
+---------+---------+
| Name | Letters |
+---------+---------+
| Ada | 3 |
| Grace | 5 |
| Adébáyọ̀ | 7 |
+---------+---------+Step 6 · Fix the errors beginners hit
gets reads a file when you pass an argument. Plain gets (without $stdin.) does not always read the keyboard: if you give the script words on the command line, Ruby treats them as file names and reads from those files. Try it:
cat > plain_gets.rb <<'RUBY'
name = gets.chomp
puts "Hello, #{name}"
RUBYruby plain_gets.rb AdaRuby stops with No such file or directory @ rb_sysopen - Ada (Errno::ENOENT). That is why greet.rb uses $stdin.gets, which always reads the keyboard.
undefined method for nil. If input ends before a line arrives (press Ctrl+D on macOS or Linux, Ctrl+Z then Enter on Windows, at the prompt), gets returns nil and .chomp fails with undefined method 'chomp' for nil (NoMethodError). Writing gets.to_s.chomp turns nil into an empty string first.
Single quotes do not interpolate. Only double-quoted strings replace #{...}:
cat > quotes.rb <<'RUBY'
name = "Kofi"
puts "Hello, #{name}"
puts 'Hello, #{name}'
RUBY
ruby quotes.rbThe first puts prints Hello, Kofi; the second prints Hello, #{name} exactly as written. Use single quotes only for text with nothing to fill in.
A missing end. Every do, if and def needs a matching end. Make a file without one:
cat > broken.rb <<'RUBY'
3.times do |i|
puts i
RUBYruby broken.rbRuby refuses to run any of the file and reports a syntax error with something like unexpected end-of-input pointing at the last line. Add end on a new line after puts i and it prints 0, 1 and 2. Indenting the block body by two spaces, as Ruby style does, makes a missing end easy to spot.
Check it worked
plain_gets.rb Ada fails with Errno::ENOENT, while greet.rb reads the keyboard. quotes.rb prints Hello, Kofi and then Hello, #{name} literally. broken.rb stops with a syntax error, and runs once you add the missing end.
When you have finished, remove the practice folder. On macOS or Linux:
cd ~
rm -rf ~/hello-ruby-demoOn Windows, delete the hello-ruby-demo folder in your user folder with File Explorer.
Next steps
- Compare Ruby with a language that borrowed its syntax but compiles to a native binary: Hello World in Crystal.
- See another language built for the web: Hello World in PHP.
- Try the same first steps in Python, Ruby's closest rival for scripting: Hello World in Python.
Related tutorials
- Hello World in Zig: zig run, zig build and zig ccYou will install a signature-checked Zig 0.17.1 on Ubuntu 26.04, write and run Hello World with the current I/O API, build a small project with zig build that handles errors explicitly, and use zig cc to compile a C program for Linux and for Windows.
- Hello World in x86-64 assembly with NASM and Linux system callsYou will write an x86-64 assembly program that prints a line using only Linux system calls, assemble it with NASM, link it with ld, check its exit status, then loop it three times and read its machine code with objdump.
- Hello World in WebAssembly text format with wabt and WasmtimeYou will write a WebAssembly module by hand in the text format, assemble it with wat2wasm, print Hello World through WASI in Wasmtime 50, and call an exported add function from both Wasmtime and Node.js.
Need this done for you?
Padding does this work for growing companies, from the first diagnosis to a system that keeps working.
Get new tutorials