Questions Concepts
GitHub icon

Ruby

Ruby - Programming language

< >

Ruby is a programming language created in 1995 by Yukihiro Matsumoto.

Source code:
git clone https://github.com/ruby/ruby
#11on PLDB 28Years Old 3mRepos

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
# This is a single line 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 · Search · Add Language · Features · Creators · Resources · About · Blog · Acknowledgements · Queries · Stats · Sponsor · Traffic · Traffic Today · Day 305 · feedback@pldb.com · Logout