Your first Ruby script

Contents
Hello file.rb
puts prints
ruby file.rb
Common beginner mistakes

Hello file.rb

A Ruby script is a plain text file with the rb extension. Open any text editor, type the line below, and save it as hello.rb in your working folder.

puts "Hello, Ruby"

That single line is a complete program. The next section explains what puts does with the string you hand it.

puts prints

puts prints text to the terminal and moves to a new line afterwards, so each call starts on a fresh line. Its sibling print writes without that final newline.

puts "one" puts "two"

one
two

ruby file.rb

Run the file by passing its name to the ruby command. The interpreter reads hello.rb from top to bottom and prints the result:

Run the script

ruby hello.rb

Hello, Ruby

The terminal prints Hello, Ruby and nothing else. No server and no browser are needed for plain scripts like this one.

Common beginner mistakes

Three mistakes catch almost every beginner. First, typing only the file name runs nothing: the ruby command must come first, as in ruby hello.rb.

Second, a typo such as put instead of puts calls a method that does not exist. Third, an unclosed quote swallows the rest of the line, so close each string on the same line you open it:

puts "Quotes come in pairs: open and close on one line"

Next: Variables

Article author: Arthur Isaev

Related articles
Ruby
Ruby course: zero to hero in 50 lessons
What is Ruby and where it runs
Install Ruby and check the version
irb: the interactive shell
Your first Ruby script
Variables
Numbers
Strings
Symbols
Truthiness: only false and nil
Conditions
Loops
Arrays
Hashes
Ranges
Methods
Blocks, procs, lambdas
String tools
Files and IO
Exceptions
Modules and mixins
Classes and objects
Inheritance
Enumerables
Duck typing and core idioms
Gems and Bundler
Testing with Minitest
Debugging
Encodings
Time and date
JSON and YAML
HTTP clients
Sockets
REST APIs
puts, print and p
Metaprogramming
Enumerators
Pattern matching
Arguments: splats and kwargs
Rake tasks
Environment and CLI
Logging
CSV
ERB templates
WEBrick server
Rails in one lesson
Capstone: todo CLI
Capstone: JSON API
Performance
Modern Ruby tour
Hero roadmap: the whole course on one page
Install Ruby on Windows 11
Install Ruby on Ubuntu
Install Ruby on Rocky Linux
Install Ruby on macOS
Install Ruby on FreeBSD
Ruby in Docker
Threads and fibers

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.ru
Write to info@urn.su, info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: