This is a Ruby syntax reference.
Learning Ruby can be overwhelming with everything you have to remember.
That’s why I put together this syntax reference for you!
It will help refresh your memory & quickly review what you need to know to write Ruby programs.
Have fun!
Contents
A string is a sequence of characters inside two quotation marks (""
). Used to represent text & data.
Syntax example:
"I like chocolate"
Another option is to use single quotation marks (''
).
'Ruby is awesome'
Important methods:
More methods:
https://www.rubyguides.com/2018/01/ruby-string-methods/
A hash ({}
) is a key-value pair (a => b
) data structure. Used as a dictionary. You can access hash elements by their keys. Keys are unique.
Example:
# Create h = { a: 1, b: 2, c: 3 } # Access h[:a] # Set h[:test] = 10
Notice that new hashes are created using {}
syntax (curly brackets), but you always access a hash element with []
(square brackets).
This simple thing confuses many beginners, so keep it in mind.
Hash with string keys:
h = { "a" => 1, "b" => 2, "c" => 3 }
Important methods:
More methods:
https://www.rubyguides.com/2020/05/ruby-hash-methods/
A static string used for identification, one common example is hash keys. They always start with a colon (:bacon
). Symbols are never used for their content (the individual characters).
When used inside hash brackets ({}
) the side of the colon is reversed.
Example:
{ abc: 1 }
This is the symbol :abc
.
Learn more:
https://www.rubyguides.com/2018/02/ruby-symbols/
A singleton class (only one object allowed) that represents a default or “not found” kind of value.
Evaluates to “false” in a conditional context.
Learn more:
An object used to represent a list of objects. An array can contain any kind of object (a = [1, "abc", []]
), including other arrays.
You access array elements with their index (a[0]
) & nested arrays with a[0][0]
.
Example:
a = [] a << 10 a << 20 a << 30 a # [10, 20, 30]
Important methods:
More methods:
https://ruby-doc.org/core-2.6.4/Array.html
A Ruby module used to iterate over the elements of any class that implements the each
method, like Array, Range & Hash.
Important methods:
More:
A Basic Guide to The Ruby Enumerable Module (+ my favorite method)
A class that helps you work with files in Ruby. Anything from reading them, writing to them, or even getting info about them, like the file size.
Example:
File.read("/tmp/test.txt")
Important methods:
More:
https://www.rubyguides.com/2015/05/working-with-files-ruby/
If you're looking to find patterns, substrings, or something specific inside a string, then a regular expression may be what you're looking for.
They can be used to validate email addresses & phone numbers. Or to extract information from text.
Example:
"aaaa1".match?(/[0-9]/) # true "".match?(/[0-9]/) # false
Learn more:
https://www.rubyguides.com/2015/06/ruby-regex/
Ruby gems are packages you can download to use in your Ruby programs.
These packages give you new functions.
For example, in Rails, you can easily add authentication with the Devise gem, or pagination with the Kaminari gem.
Learn more:
https://www.rubyguides.com/2018/09/ruby-gems-gemfiles-bundler/
Ruby is an Object-Oriented Programming language. We think of everything as an object. Objects are created from their blueprints, classes.
Objects can know things & do things. You tell objects to do things with methods.
Important methods:
Learn more:
A variable is a label for an object that we can use to access that object. The process of associating a variable with an object is called "variable assignment".
Example:
a = 1
We use different kinds of variables in Ruby.
Here's a list:
something
)@something
)Something
/ SOMETHING
)$something
)The main difference is from what locations you can access them.
There is a way to create objects with a special kind of syntax, the percentage symbol (%
). This a shortcut that can save you work.
If you see %w
in Ruby, now you'll know what it means!
Examples:
array_of_strings = %w(apple orange coconut) array_of_symbols = %i(a b c) string = %q(things) regular_expression = %r([0-9])
Remember that the percentage symbol (%
) is also used as the modulo mathematical operator.
Parenthesis & semicolons are not required in Ruby, but they can be used.
Some basic rules:
def foo
def foo(a, b, c)
(a.size + b.size) * 2
Readability is one use for parenthesis while changing the order of operations is another.
Examples of common Ruby syntax.
def backup_database # method body end
More about methods:
class Fruit # methods end
More about classes:
true ? "yes" : "no"
fruit = "orange" puts "I have an #{fruit}. Would you like a slice of it?"
More about interpolation:
[1,2,3].each do |n| puts n end
n = 20 if n > 1 puts "Greater than 1" else puts "Less than 1" end
case 20 when 1..20 puts "Between 1 & 20" when 21..40 puts "Between 21 & 40" else puts "Not within a valid range" end
Where 1..20
is a Range
object.
You learned the basics of Ruby syntax so you can write Ruby programs! Review these often until it becomes built into your brain.
You may also enjoy this list of common names for syntax elements.
Thanks for reading. 🙂