Learn Ruby from Scratch
Learn Ruby from Scratch
These notes are taken from this Educative online course, which I audited for free.
1. Why should you learn Ruby?
Programming is Creation
Motivate yourself to bring your code to life!
when you run a program, a universe is created where things come to life and interact with each other
as the creator, you define the rules
e.g. creating an application like Twitter
create users
users can create tweets
users can follow each other
each time new users are created in your application, users have the ability to tweet and follow each other
2. Variables
What are Variables?
This lesson gives an insight into variables and variable assignment.
Naming Things
variables: a way to assign names to objects that our program deals with
Variable Assignment
in Ruby, you can assign a name to an object by using the assignment operator
=
Output
this assigns the name
number
to the object that is the number1
after, we can refer to this object by using the name
number
Output
Variable is not a "thing"
a variable is not an object by itself
it's just a name for an object
in the example, the number
1
is an object, whilenumber
is a name for it because we've assigned it
Output:
try and pick names that reveal your intention
from the example, using
a
,large_number
, andapples
as names would be frowned upon because they aren't meaningful names and don't match the objecta variable name is like a post-it note with the name
number
written on it and stuck on the object, the number1
Reusing Variable Names
This lesson explains how variable names can be reused in Ruby
a name is unique
the same name can only be assigned to one object at a time
assigning different values to the same variable results in later assignments overwriting previous ones
variable names can be re-used and re-assigned
Output
continuing the post-it metaphor, this would stick a post-it with the name
number
on an object, and then later take it off and stick it on another object
Explanation
Ruby creates the number
4
on the first line and assigns the namenumber
to iton the second line, Ruby evaluates the expression
number * 3
, resulting in a new number12
that is assigned to thenumber
variableon the third line Ruby adds
2
to thenumber
object, resulting in14
Ruby passes
14
toputs
, which outputs it to the screenyou can write the exact code with
puts 4 * 3 + 2
however, using variable names can be useful to break up long lines and make code more expressive and readable
this is a local variable and is the one that's used most often
Things on the Right go First
This lesson describes how Ruby figures out the expression on the right first.
Example
Ruby needs to know what the object on the right is before it can assign the name
number
to itRuby first evaluates the expression, which results in the number
14
Ruby then assigns the name
number
to the objectthe code temporarily looks like
number = 14
before the assignment=
is evaluated
Does this make sense?
on the second line, Ruby passes the
number
variable (which is14
) toputs
, which outputs to the screen
Types of Variables and their Usage
Get to know various kinds of variables in Ruby. Moreover, learn how and when to use them.
Definition and Usage
Naming | Scope | Initialization | |
Global Variables | Global variables start with a | Their scope is global which means that they can be accessed from anywhere in a program. | There’s no need to initilize. Uninitialized global variables have the value nil. |
Local Variables | Local variables begin with a lowercase letter or underscore( | The scope of a local variable ranges from class, module, def, or from a block’s opening brace to its close brace {}, i.e within its block of initialization. | No need to initialize. An uninitialized local variable is interpreted as methods with no arguments. |
Instance Variables | They start with an | Their scope is limited to one instance of a class. | There’s no need to initilize. Uninitialized instance variables have the value nil. |
Class Variables | Class variables start with an | Their scope is limited to the class in which they are created. | They need to be initialized beforehand, otherwise they’ll result in error. |
Examples
Global Variables
$global_var
is a global variable since it is preceded by a$
signit can be accessed from anywhere in the program
non_global_var
is a simple variablecannot be accessed from inside the
method2
so it gives an "undefined local variable or method" error
Output
Local Variables
there are two variables named
var
with different scopesthe
var
inside the method has the local scope
Output
Instance Variables
an instance variable starts with an
@
signit belongs to only one instance of the class
an uninitialized instance variable will have a
nil
value
Output
Class Variables
a class variable starts with a
@@
signit should be initialized before use
it belongs to the whole class and can be accessed from anywhere in the class
a class variable is shared by all the descendants of the class
its value will change for every instance of the class
Output
Quick Quiz on Variables!
The quiz is located here.
3. Built-in Classes: Numbers
Working with Numbers
This lesson goes through the types of number evaluations in Ruby.
🔢 Numbers are Simply Numbers
you can create a number by writing it e.g.
123
negative numbers are created by prepending a minus
-
you can create decimal numbers by writing them e.g.
12.34
you can use an underscore
-
to separate thousands places e.g.1_234.56
which is equal to1234.56
a number is defined by a series of digits, using a dot as a decimal mark, and optionally an underscore as a thousands separator
Kinds of Numbers
there are different kinds of numbers
integer numbers aka "integers"
floating point numbers aka "floats"
if you do a calculation with integer numbers you'll always get an integer back
if there is a float, you'll get a float back
mathematical operations result in a floating point number except if all numbers used are integer numbers
this is important when you do a division (
/
means "divide by")
decimal places will be cut off
use floating point (decimal) numbers when doing divisions
Further Readings
look through the documentation for integer numbers
Exercise 1: Playing with Numbers
Problem Statement
In the exercise below, calculate:
how many hours are in a year?
how many minutes are in a decade?
how to convert an age given in years seconds
Try it yourself
3 of 3 Tests Passed
Exercise 2: Guess the Type?
The quiz is located here.
Exercise 3: Finding Modulo
Problem Statement
Find out what "modulo" means by asking Google and then calculate the modulo of two numbers given as an input.
Try it yourself
5 of 5 Tests Passed
Exercise 4: Even or Odd?
Check whether a number is even or odd.
Problem Statement
Use a method from the documentation to find out if given numbers are odd or even.
5 of 5 Tests Passed
4. Built-in Classes: Strings
Working with Strings
This lesson will teach you how you can operate on strings in Ruby.
🔤 A String, in programming languages, is text.
a String is an object that represents a specific
the most simple and common way to create Strings is to enclose some characters in quotes
you can use single or double qoutes
Things you can do with Strings
you can stick them together by using
+
this is called concatenation
you can replicate the last operation using
*
multiplying a String by a number in Ruby means repeating the String as many times
Ruby behaves the same for Strings that contain nothing but numbers
some other String methods:
the last
Last updated