Top 1,000 Features Creators Events Podcasts Extensions Interviews Blog Explorer CSV

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
Homepage · Leet Sheet · REPL · Try It Online · Download · Source Code · Spec · Blog · Wikipedia · Subreddit · FAQ · Release Notes · Docs · Mailing List

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 Example Token
Standard Library
puts "Hello, World!"
Conditionals
Functions
While Loops
Case Sensitivity
Range Operator
(3...6)
Line Comments
# A comment
#
Print() Debugging
puts "Hi"
puts
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
A comment.
=end
=begin =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 · Resources · Acknowledgements · Part of the World Wide Scroll