Ruby
lastmod 2026-04-27created 2015-06-0734 edits
Ruby is a dynamic, object-oriented scripting programming language developed by Yukihiro Matsumoto (松本行弘). Ruby is a pure object-oriented language. Everything in Ruby, including data types such as integers and strings, is an object.
Features include class definitions, garbage collection, powerful regular expression handling, multithreading, exception handling, iteration, closures, Mixin, operator overloading, and more. The syntax inherits from the ALGOL family while emphasizing readability.
For a long time the language specification was not written down and was considered to follow Matsumoto's implementation, but since June 2010 the language specification has been in the works as RubySpec.
Categories
Syntax
- Code Block & Yield
- Ruby:Symbol
- Ruby:Mixin
Libraries
- timetrap - Timetrap is a simple command-line time tracker written in Ruby. It provides an easy-to-use command-line interface for tracking the time you spend.
Variables
| Signature | Brief | Example |
| $ | Global variable | |
| @ | Instance variable; simply put, a class member variable | |
| [a-z_!] | Local variable || name = 'unknown' | |
| [A-Z!] | Constant || NAME = "KIM" |
Special variables
-
$!: last error message -
$@: error location -
$_: string most recently read by gets -
$.: line number of the code -
$&: string last matched by regexp -
$~: the last regexp match, as an array of subexpressions -
$n: nth matched string -
$=: case-insensitivity flag -
$/: input record separator -
$\: output record separator -
$0: name of the running program -
$*: command-line arguments -
$$: process ID -
$?: exit status of the most recently run child process -
ARGV[n]: nth command-line argument -
$DEBUG: debugging messages; enabled when -d is turned on -
$stderr: standard error -
$stdin: standard input -
$stdout: standard output -
$:: names of modules loaded by the script
Comment
Characters after # are treated as a comment. Alternatively, you can comment out a block with =begin and =end.
Evaluate String
A string enclosed in single quotes (') means its contents make up the string as is. A string enclosed in double quotes (") evaluates its contents.
Iterators and Blocks
Think of an Iterator as an object for cycling through data. In Ruby, arrays also have an Iterator. Use .each.
presidents = ["Ford", "Carter", "Reagan", "Bush1", "Clinton", "Bush2"]
presidents.each {|prez| puts prez}
## OR:
presidents.each do |prez|
puts prez
end
{} vs do/end
{} applies to the result of the Iterator, while do/end applies to the Iterator.
Create Proc
List of methods
## Total methods.
Greeter.instance_methods
## Ignore parent methods.
Greeter.instance_methods(false)
Methods checker
irb(main):041:0> g.respond_to?("name")
=> false
irb(main):042:0> g.respond_to?("say_hi")
=> true
irb(main):043:0> g.respond_to?("to_s")
=> true
Setter & Getter
irb(main):044:0> class Greeter
irb(main):045:1> attr_accessor :name
irb(main):046:1> end
irb(main):047:0> g = Greeter.new("Andy")
=> #<Greeter:0x3c9b0 @name="Andy">
irb(main):048:0> g.respond_to?("name")
=> true
irb(main):049:0> g.respond_to?("name=")
=> true
irb(main):051:0> g.name="Betty"
=> "Betty"
irb(main):052:0> g
=> #<Greeter:0x3c9b0 @name="Betty">
irb(main):053:0> g.name
=> "Betty"
irb(main):054:0> g.say_hi
Hi Betty!
=> nil
Frameworks
See also
- Duck typing
- Bundler
- Rake
- Gem
- RVM
- rbenv
- Artichoke - Ruby made with Rust
- RubyLLM - Working with AI the Ruby way
- Spinel - Ruby AOT native compiler
Favorite site
- Ruby web site
- To Ruby From Other Languages
- Getting started with programming in Ruby 1
- Korean documentation for the Ruby programming language
Basic tutorial
- [Recommended] Joinc: Ruby Tutorial 01 - Ruby language tutorial 2
- [Recommended] Joinc: Ruby Tutorial 02 - Using Ruby classes 3
- Ruby basic syntax summary
- Ruby - Basic syntax
- Ruby 1.8.x syntax summary
- Ruby syntax lessons: Output statements - 1. Hello Ruby
- Ruby basic syntax summary
- Ruby classes, objects, variables
Article
- [Recommended] Why does Ruby use so much memory? (covers OS and interpreter memory management, paging and paging pools; memory fragmentation, etc.)
References
- Artichoke
- Bundler
- Computer Language
- Continuous Integration
- Documentation generator
- Domain-specific language
- Dwm
- Elder
- Eleventy
- Fusion.ssg
- GatsbyJS
- Gitlab.rb
- Gitmal
- Here document
- Hexo
- Hugo
- Jamstack
- JavaScript:Libraries
- Jekyll
- Lusift
- Meli
- Metascraper
- Node.js
- Nvidia-smi-exporter
- Octopress
- Package management system
- Pacman
- Pelican
- Programming
- Programming language
- Prometheus
- Prometheus:Exporters
- Python
- Rails:Route
- Rake
- Rbenv
- Ronin
- Routing engine
- Ruby on Rails
- Ruby Version Manager
- Ruby:Mixin
- Ruby:Symbol
- RubyGems
- RubyLLM
- Rust
- Scripting Language
- Spinel
- Static Site Generator
- Timetrap
- Vagrantfile
- Vim:Plugin
- Vim:Plugin:vim-rails
- VuePress
- Web application framework
- Web development
- YARD
- ZBar
- Zola