Ruby Features Reference
Intro
This is not a detailed info on each language feature. This guide is meant to be a quick reference to refresh your mind on how to use language features. I use different language when coding. This is a good way for me to remember syntax on how a specific language feature is used.
Comments
# inline comment
puts "this works also" # inline comment
=begin
Multiline
Comment
=end
Data Types
# Integers
123
1_000
-123
# Floating Numbers
123.4
-12.3
# Strings
"hello"
'hi'
# Symbols
:hello # Like string hello but as code :)
# Arrays
[1, 2, 3]
arr = ["hello", 1, :hello, true]
arr[0] # => "hello" (Zero based array)
arr[4] # => nil (When out of bound returns nil)
[ # nester arrays
[1, 2, 3],
[4, 5, 6]
]
# Hash
{ "message" => "hello", "response" => "hi" }
{ :message => "hello", :response => "hi" }
{ 1 => "one", 2 => "two"}
dict = { message: "hello", response: "hi" } # modern syntax
dict[:message] # => "hello"
dict[:unknown] # => nil (Not found in hash returns nil)
# Ranges
(1..4) # 1, 2, 3, 4, 5
(1...4) # 1, 2, 3
('a'..'c') # 'a', 'b', 'c'
# Misc
true
false
self # Object that owns executing code.
nil # Value that represents nothing.
__FILE__ # The name of the source file.
__LINE__ # Line number on source file.
Variable Scopes
Global Variable
Variables where scope is shared by whole runtime. On this sample counter can be incremented even outside the class.
$global_counter = 0
class Counter
def increment
$global_counter += 1
puts "Counter is #{$global_counter}"
end
end
counter1 = Counter.new
counter2 = Counter.new
counter1.increment # prints Counter is 1
$global_counter += 1
counter2.increment # prints Counter is 3
Instance Variable
Variables where scope is within an instance. On this sample all instances have its own copy counter variable.
class Counter
def initialize
@instance_counter = 0
end
def increment
@instance_counter += 1
puts "Counter is #@instance_counter"
end
end
counter1 = Counter.new
counter2 = Counter.new
counter1.increment # prints Counter is 1
counter2.increment # prints Counter is 1
Class Variable
Variables where scope is shared between class instances. On this sample class_counter
is shared on all instances of counter.
class Counter
@@class_counter = 0
def increment
@@class_counter += 1
puts "Counter is #{@@class_counter}"
end
end
counter1 = Counter.new
counter2 = Counter.new
counter1.increment # prints Counter is 1
counter2.increment # prints Counter is 2
Local Variable
Variables where scope is within current block. On this sample we used local variable on initialize which scope is limited to that method. When increment is called it will fail since it does not exist on that block.
class Counter
def initialize
local_counter = 0
end
def increment
local_counter += 1
puts "Counter is #{local_counter}"
end
end
counter = Counter.new
counter.increment # Will fail because local_counter is nil.
Class
Defining Class
class Counter
@@class_counter = 0
def initialize(start)
if start
@@class_counter = start
end
end
def increment
@@class_counter += 1
puts "Counter is #{@@class_counter}"
end
def increment_by(inc)
@@class_counter += inc
puts "Counter is #{@@class_counter}"
end
end
counter = Counter.new(1)
counter.increment # prints Counter is 1
counter.increment_by 2 # prints Counter is 3
Readers/Writers (Getter/Setter)
In ruby instance variables are protected (Not private!). You need to specify getter and setter methods to make it accessible outside of object. This is useful to add logic before reading and writing values.
class Counter
def initialize
@value = 0
end
def value # reader method
@value
end
def value=(value) # setter method
raise "Value not an integer" unless value.is_a?(Integer)
@value = value
end
def increment
@value += 1
puts "Counter is #@value"
end
end
counter = Counter.new
counter.increment
counter.value = 3
puts counter.value # prints "3"
counter.value = "Not a number" # Error with "Value not an integer"
Modules
Define a namespace as a way to group together classes, methods, and constants.
module ModuleName
CONSTANT_VALUE = 1
...
end
# loading modules
require 'module_name'
require_relative './relative_path'
load 'filename.rb' # Reload module everytime.