Skip to main content

Sponsors

# A function.
=begin
Multi-line comment.
But =begin and =end must be at the beginning of the lines.
=end
def work(name = "yman")
        result = "name: " + name
        return result
end
puts work("xman")
print "hello world\n"
printf "hello world %d\n", 100
# Output:
name: xman
hello world
hello world 100

name = gets()
puts "hello world, #{name}"
puts "hello world, #{(1+2)*3}"
# Output:
hello world, xman
hello world, 9

# Iterators.
a = [1,2,5,6]
a.each do |i|
        puts i*2
end
# Output:
2
4
10
12
a = [1,2,5,6]
a = a.select do |i|
 i % 2 == 0
end
puts a
# Output:
2
6

# Exceptions.
begin
 f = open("info.dat")
rescue
 puts "#{"info.dat"} not found"
 exit 1
end
# Output:
info.dat not found

# While.
i = 0
while i <= 100 do
  if i % 2 == 0 then
    puts i
    i+=1
  elsif i % 3 == 0 then
    puts i
    i+=3
  else
    puts i
    i+=2
  end
end

x = 0
begin
  x = x + 1
  puts x
end while x < 10

# Unless.
x = 20
y = 3
exit unless x > y
unless x > y then
  puts x
else
  puts y
end

# Until.
x = 0
until x > 10 do
  puts x
  x+=1
end

# Case.
x = 5
case x
when 1,2,3 then
  puts 1
when 4,5,6 then
  puts 2
else
  puts 3
end

# For.
a = [[1,4],[3,6],5]
for i,j in a do
        puts i,j
end

# Other statements.
# break, next, redo, retry, begin, rescue, raise, BEGIN, END.
BEGIN { # Execute before program run.
        code
}

END {   # Execute at end of program.
        code
}

# Whitespace.
a + b # => a+b, a is local var.
a +b  # => a(+b), a is a method.

# Reserved words: BEGIN END alias and begin break case class def defined?
# do else elsif 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 __FILE__ __LINE__

# Numbers.
123
1_23
0233
0xff
0b1001
?a
1223344556677
1.23
1.1e2
2E10
2e+10

# Strings.
"abc"
'abc'
a = "abc" 'abc'

# Expression substitution.
#$var #@var #{$var} #{@var} ?????

# Run command.
s = `ls`

# Delimited strings.
str = %!abc!    # "abc"
str = %Q!abc!   # "abc"
str = %q!abc!   # 'abc'
str = %x!ls!    # ls output string.

# Multi-line string.
# str is a delimiter, which can be any string,
# also is a string indicating start and end of the multi-line string.
s = < v1, k2 => v2}
s = {"k1" => v1, "k2" => v2}

# Regular exression.
/pattern/
/pattern/OPTIONS
%r!/abc/def!

# Variables.
$abc    # Global variable.
@abc    # Instance variable.
@@abc   # Class variable.
abc             # Local variable.
_aaa    # Local variable.
Abc             # Constant.

# Assignment.
a = b
a, b = 10, 20   # Parallel assignment.

# Other assignments.
+= -= *= /= %= **= <<= >>= &= |= ^= &&= ||=

# Range operator.
e1 .. e2        # e1 <= x <= e2
e1 ... e2       # e1 <= x < e2

# Ternary operator.
a ? b : c

# defined? operator.
defined? var
defined? puts
defined? super

# Method calls.
# arg = [expr... [, *expr [, &expr]]]
# *expr: Addition arguments in array.
# &expr: Proc object converted from the block.
obj.method(arg)
obj.method arg
obj::method(arg)
obj::method arg

# Block.
method {[|[var[,var...]]|] code}        # higher precedence.
method do [|[var[,var...]]|] code end

# Singleton method. Define method of an object.
s = "abc"
def s.work(n)
        puts n
end
s.work(10)

# Alias for method.
def work(n)
        puts n
end
alias work2 work
work2(20)

# Undefine method.
undef work

# yield, super
yield([expr ...])
super([expr ...])

# Import.
require "hello"
load "hello.rb"

# Class.
class A
  def work()
    puts "hello"
  end
end
class B<A
  def initialize
    @var = 0
  end
  attr_accessor :name
  attr_reader :value, :value2
  attr_writer :value, :value2
  def var()
    return @var
  end
  def var=(v)
    @var = v
  end
  def work2()
    puts "hello2"
  end
end
c = B::new()
c.work2()
c.var = "hello world"

# Module.
module Mname
        puts "test"
end

# Method visibility.
public, protected, private.

# Hooks: Notify when events such as defining instance,
# singleton and make subclass happen.
class A
  def A::inherited(sub)
    str = sub.name
    str += " inherited from A"
        puts str
  end
end
class B<A

Reference: Ruby In A Nutshell A Desktop Quick Reference by Yukihiro Matsumoto