Ruby

string

string literal

simple string

'parsed as is'

string considering \

"string where \nis parsed"

formatted string

str = "yes
"say #{str}"

match string

match a string with regex and return index of match

str =~ regex

match string with regex and name each part

comp = /\[(?<comp1>.*)\](?<comp2>.*)/.match(str)
comp1 = comp[:comp1]
comp2 = comp[:comp2]

get all word from string

str1.split(/[^[[:word:]]]+/)

array

arr = [ "a", 1, true ]
arr[0] # => "a"
arr[1, 2] # => [ 1, true ]

array method

.length .push(ele) .pop

# loop through
arr.each do |ele|
    # …
end
# join with `str`
arr.join(str)
# map
arr.map do |ele|
    # …
end
# map with index
arr.map.with_index do |ele, index|
    # …
end

string array

%w(this will be split) # => [ "this", "will", "be", "split" ]

hash

# old way
ha = {
    'a' => 1,
    'b' => 2
}
ha['a'] # => 1
ha['c'] # => nil
# new way
ha = {
    'a': 1,
    'b': 2
}
ha[:a] # => 1

block

{ |args|
    # …
}
# or
do |args|
    # …
end

call block

yield {|ele| puts ele}

control flow

if

if a
    # …
elsif b
    # …
else
    # …
end

case

case a
when b
    # …
when c
    # …
end

while

while a
    # …
end

statement modifier

do_something if a

function

yield execute a block

eval evaluate a string as code

define function

def func1(arg = default_value)
    # …
    # return last expression
end

main function

if __FILE__ == $0
    # main function
end

class

class ClassName
    # called with `new`
    def initialize(arg)
        # …
    end
end

instance variable

@name

access instance variable

attr_accessor :name

method

# all instance method of class
ClassName.instance_methods
# instance method excluding inherited
ClassName.instance_methods(false)

inherited method

.respond_to?(str) check if object has method str

.to_s convert to string

.nil? check if nil

static method

def self.static_method_name(arg)
    # …
end

safe navigation syntax &.

same as optional chaining

module

module ModuleName
    def self.static_method1
        # …
    end
end

exception

class CustomError < RuntimeError
end