Top 1,000 Features Creators Resources Extensions Blog Explorer Download
GitHub icon

Ruby

< >

Ruby is an open source programming language created in 1995 by Yukihiro Matsumoto.

#9on PLDB 29Years Old 3mRepos
Download source code:
git clone https://github.com/ruby/ruby

Try now: Riju ยท TIO ยท Replit

Ruby is a dynamic, reflective, object-oriented, general-purpose programming language. It was designed and developed in the mid-1990s by Yukihiro "Matz" Matsumoto in Japan. According to its creator, Ruby was influenced by Perl, Smalltalk, Eiffel, Ada, and Lisp. Read more on Wikipedia...


Example from Compiler Explorer:
# Type your code here, or load an example. def square(num) num * num end
Example from Riju:
puts "Hello, world!"
Example from hello-world:
#!/usr/bin/env ruby print "Hello World"
# Hello World in Ruby puts "Hello World!"
Example from Linguist:
module Foo end
Example from Wikipedia:
"Hello, World!".in_blue => "<span style=\"color: #00f\">Hello, World!</span>"
__ENCODING__ __FILE__ __LINE__ alias and BEGIN begin break case class def defined do else elsif END end ensure false for if in module next nil not or redo rescue retry return self super then true undef unless until when while yield

Language features

Feature Supported Token Example
Conditionals โœ“
Functions โœ“
While Loops โœ“
Case Sensitivity โœ“
Range Operator โœ“
(3...6)
Line Comments โœ“ #
# A comment
Print() Debugging โœ“ puts
puts "Hi"
Operator Overloading โœ“
Multiline Strings โœ“
Mixins โœ“
module A
   def a1
   end
   def a2
   end
end
module B
   def b1
   end
   def b2
   end
end
class Sample
include A
include B
   def s1
   end
end
samp = Sample.new
samp.a1
samp.a2
samp.b1
Iterators โœ“
(0...42).each do |n|
  puts n
end
File Imports โœ“
load 'filename.rb'
require 'filename'
require 'trig.rb'
Directives โœ“
coding: UTF-8
Here Document โœ“
Generators โœ“
# Generator from an Enumerator object
chars = Enumerator.new(['A', 'B', 'C', 'Z'])

4.times { puts chars.next }

# Generator from a block
count = Enumerator.new do |yielder|
  i = 0
  loop { yielder.yield i += 1 }
end

100.times { puts count.next }
Garbage Collection โœ“
MultiLine Comments โœ“ =begin =end
=begin
A comment.
=end
Comments โœ“
# A comment
Classes โœ“
# A tiny Person class in Ruby:
class Person
  attr_accessor :name, :age, :   # getter and setter methods
  def initialize(name, age)
    @name = name
    @age = age
  end
end

# Create a new Person object:
person = Person.new("John", 30)
puts person.name
puts person.age

# Change the age of the person:
person.age = 35
puts person.age
Constants โœ“
# Constants in Ruby being with a capital letter. Will throw a warning or error, depending on runtime settings.
Name = "John"
# Not a constant
name = "John"
Exceptions โœ“
def hello
 puts "Hello, World!"
   # start an exception handler
   begin
       raise "This is an exception"
   rescue => e
       puts "Exception caught: #{e}"
   end
 end
hello
Assignment โœ“
pldb = 80766866
Booleans โœ“ true false
Integers โœ“
pldb = 80766866
Duck Typing โœ“
Regular Expression Syntax Sugar โœ“
puts "Hello World".match(/\w/)
Strings โœ“ "
"hello world"
Case Insensitive Identifiers X
Semantic Indentation X
Async Await X
Function Overloading X
Dispose Blocks Pattern X
Dependent types X
Enums X
Variable Substitution Syntax X

View source

- Build the next great programming language ยท About ยท Acknowledgements ยท Published by Breck's Lab