This presentation is available at: https://presentations.generalassemb.ly/2e967bd4959638cac82f
After this lesson, students will be able to:
-
Identify Ruby's data types
-
Use variable naming to determine a variable's scope
-
Run a Ruby program in the terminal
-
We've gotten our feet wet with the in-browser technologies:
- HTML, CSS, and JavaScript
-
This week we are going to learn a new programming language called Ruby.
-
After learning the basics of Ruby, we'll look at how to use the language, along with the extremely popular Ruby on Rails framework, to write full-stack web apps!
Pair up and research the background and other facts you can learn about the
"Ruby Programming Language"
in 2 minutes
-
Avoid information related to Ruby on Rails - we're only interested in the Ruby programming language!
-
Be prepared to share your findings...
-
Created By: Yukihiro "Matz" Matsumoto (of Japan)
-
When Created: mid-1990's
-
Characteristics:
- Great readability - designed for programmers
- Feels "natural" in a way that mirrors life
- Fun to use, less typing
- OOP Language - everything is an Object
> "I hope to see Ruby help every programmer in the world to be productive, and to enjoy programming, and to be happy. That is the primary purpose of Ruby language."
Matz
-
Interactive Ruby Shell
- IRB - the default
- Pry - an improved shell that was installed during Installfest
-
Open a Terminal window and type
IRB
Welcome to your Ruby playground! -
Type:
"Hello" + " World!"
=> "Hello World!"
-
Good to go!
-
You've already been exposed to several programming concepts that are common to all programming languages:
- Datatypes & variables (including scope)
- Flow control
- Comparison expressions
- Functions/Methods
- Etc.
-
You've become familiar with JavaScript - perhaps your first programming language. So...
-
A good starting point for your journey to become a Rubyist is to compare some of the basics of Ruby to that of JavaScript's...
-
Everything in Ruby is an Object!
"hello".methods => wow!...
When you get tired of scrolling down through the list of methods with the arrow key, pressing
q
will return to command mode.
-
Unlike JavaScript, in Ruby, every "property" on an object is a method, it never contains data.
-
Don't misunderstand, objects contain data, it's just that the data needs to be set and retrieved using method calls.
-
Enough about objects for now, we'll cover them more in detail in upcoming lessons.
-
Take a look again at what we typed earlier:
"hello".methods
-
Notice anything different as compared to JavaScript?...
-
In Ruby, semicolons are optional and rarely used!
-
Typically, you would only use semicolons to separate multiple statements written on the same line, which is usually a bad idea.
For example (don't try to run):cat.name = "Felix"; cat.age = 3
-
We can invoke methods without parenthesis, and commonly do!
my_car.make = "Toyota"
Don't let the
=
fool you, we are not assigning "Toyota" to amake
property, we are actually calling a method namedmake=
passing in "Toyota" as an argument. The following three statements do the exact same thing:my_car.make=("Toyota") # parens are optional my_car.make= "Toyota" # space allowed thanks to syntactic sugar my_car.make = "Toyota"
-
Type this into our REPL window:
x = 5 puts x x += 3 puts x
Note: The
puts
method outputs to the standard output and appends a newline character. The
- Contrast the above with JavaScript...
-
Variable and method names are typically snake-cased, not camel-cased as in JS.
-
There is no
var
or equivalent keyword in Ruby. Variables are automatically declared when they are used for the first time. -
The
+=
(in place addition) operator works the same as it does in JS. You will find that the majority of operators work like they do in JS.
-
In classical OOP languages such as Ruby, all objects are created by Classes. Think of classes as a blueprint for objects.
-
An object's class, is in essence, it's type.
-
Ruby has built-in classes that map very closely to the datatypes we saw in JavaScript.
-
Let's start with a quick review of the datatypes we've seen in JavaScript.
-
? - How many datatypes are there in JS?
-
I'll write them on the board as you call them out...
- boolean (
true
orfalse
) - number (
12
,3.95
, etc.) - string (
"awesome"
) - object
{key: 'value', nice: true, stuff: [1,2,3]}
- Built-in special objects
(arrays, dates, functions, regex)
- null (used to represent no value explicitly)
- undefined (uninitialized variable or nothing returned)
#### Let's see how Ruby's datatypes compare...
-
Booleans are the same as in JS because they contain either
true
orfalse
. -
A key difference however is in truthy and falsy comparisons:
!!0 => true # zero is false in JS !!"" => true # empty string is false in JS
-
Just remember, everything in Ruby is truthy except:
false
nil
-
Unlike JavaScript with its sole
number
datatype, Ruby distinguishes internally between floats and integers.15.class => Integer 12345678912345678912.class => Integer
-
Since everything in Ruby is an object, we can find out what class was used to create that object by calling its
class
method. -
You may see
Fixnum
orBignum
instead ofInteger
if your Ruby version is not up to date.
-
The class used to create floats is
Float
:2.34.class => Float
-
Ruby automatically converts one type of number to another for us. Try this:
my_number = 15 + 5.2 => 20.2
-
What will the datatype of
my_number
be?
-
Check this out:
5 / 2 => 2
Wow, we divide two integers and the result is an integer, which may not be what you expect!
-
Now try this:
5 / 2.0 => 2.5
That's better :)
-
Strings are pretty much the same as in JavaScript but with a slew of more methods available.
-
Just like in JS, we can use single or double quotes for string literals. Double quotes are a little more popular due to string interpolation.
-
It's okay if you don't remember string interpolation, we'll review it in a bit...
-
Unlike JS, Ruby will not automatically convert numbers into strings for us:
age = 21 "She is " + age.to_s + " years old" => "She is 21 years old"
-
Every object in Ruby has the
to_s
method. -
to_s
has a default implementation, but its behavior can be overridden (just like any method can be).
-
With String Interpolation, Ruby will evaluate an expression embedded in a string and automatically call
to_s
on it:more_years = 5 "In #{more_years} years, she will be #{age + more_years} years old" => "In 5 years, she will be 26 years old"
-
Multiple
#{}
sequences can be used in the same string. -
Double quotes (
"
) must be used with interpolation. -
FYI, there are no template strings (back-tick) like we saw in ES2015.
-
Arrays in Ruby are similar to JS, but of course with more methods.
-
Just like in JavaScript, they can be created using an array literal and indexed with bracket notation:
my_array = ['Hello', 'World'] my_array[0] + " " + my_array[1] => "Hello World"
-
More on Arrays and Ruby's Symbols & Hashes (closest thing to JS's plain objects) coming later today!
-
The equivalent of JavaScript's
null
in Ruby isnil
-
There is no
undefined
in Ruby. If you access a variable that has not been initialized by assigning a value to it, it returns an error.
-
So we know that arrays are awesome at storing lists of data elements. However, we also know that a great data structure to have around is one that holds key/value pairs.
-
What data type did we use in JavaScript when we needed to store key/value pairs?
-
In CS terms, this data type is known as a dictionary.
-
In Ruby, we use a hash to hold key/value pairs.
-
Here's how we might create a hash in Ruby:
person1 = {'name' => 'Suzy', 'age' => 23} => {"name"=>"Suzy", "age"=>23} person1['name'] => "Suzy" person1.keys => ["name", "age"] person1.values => ["Suzy", 23]
-
Note how square bracket notation must be used to access the values in a hash - there is no dot notation:
person1 = {'name' => 'Suzy', 'age' => 23} => {"name"=>"Suzy", "age"=>23} person1['name'] => "Suzy"
-
Why would dot notation not be possible?
-
The
=>
is called a hash rocket, and it is used to designate what value the key holds ("points to"):person1 = {'name' => 'Suzy', 'age' => 23} => {"name"=>"Suzy", "age"=>23} person1['name'] => "Suzy"
-
Ruby version 1.9 presented a more concise way to write hashes using another data type known as Symbols...
-
Symbols allow for a more concise syntax:
person2 = {name: 'Joe', age: 12} => {:name=>"Joe", :age=>12} person2[:age] => 12
-
With a colon to the right of the key name, using symbols this way will feel a lot like working with JS objects.
-
Except when using the concise syntax to assign values within a hash, symbols actually begin with a colon, for example, look how Ruby returned the hash we defined:
person2 = {name: 'Joe', age: 12} => {:name=>"Joe", :age=>12}
-
And how we used the symbol within the brackets to access the value:
person2[:age] => 12
-
The newer concise syntax will save you some typing, however, you will see this "longer" syntax using the hash rocket to define hashes in plenty of existing code bases and when googling:
person2 = {:name => 'Joe', :age => 12} => {:name=>"Joe", :age=>12}
-
Think of symbols as efficient strings used to identify stuff.
-
Ruby uses them internally all over the place because of their efficiency:
"hello".object_id => 70345675970460 "hello".object_id => 70345675973588 :hello.object_id => 1088348 :hello.object_id => 1088348
-
Notice each time we use a string with the same text, it creates a new object in memory. Symbols on the other hand are only created once, saving memory, and increasing execution speed.
-
Is the value of 0 (zero) considered truthy or falsey?
-
In Ruby, everything is an ________.
-
String ____________ allows us to embed an expression within a string.
-
Assuming the following code:
s = "apples: " n = 25 p = s + n
What is the result in Ruby?
In JavaScript?
-
Review of Scope:
- The scope of a variable determines where in the code the variable is accessible.
- If you can access a variable (or method), it's considered to be in scope, otherwise it's out of scope.
Variables in JavaScript had either _______ or _______ scope.
-
Levels of Scope in Ruby:
- Global
- Local
- Instance
- Class
-
In Ruby, variables are scoped according to the way they are named!
-
If the Variable identifier begins with:
[a-z]
or_
- Local variable$
- Global variable@
- Instance variable@@
- Class variable[A-Z]
- A constant (class/module/global)
-
A local variable is only available to the block of code or method it's defined in.
num_cars = 3 def get_car_count return num_cars # FYI, the return keyword is optional :) end num_cars => 3 get_car_count => NameError: undefined local variable or method `num_cars'...
-
Local variables are typically used to hold temporary data within methods.
-
Global variables are in scope everywhere within your program.
$name = "Rose" def say_hello "Hello #{$name}" end say_hello => "Hello Rose"
-
As usual, the use of global variables should be minimized.
-
An instance variable is in scope inside of an instance of a class.
-
Tomorrow, we will look at defining classes and instance variables. For this example, just realize that we are actually in an instance of a class when using
IRB
:@apples = 25 def pick_apples(num_picked) @apples += num_picked end pick_apples 5 => 30 @apples => 30
-
The lifetime of an instance variable is the same for the object instance itself.
-
Class variables (defined using
@@
) live inside of the class and are in scope to all instances of that same class. -
There is only one copy in memory of a given class, therefore there is only one copy of a class variable.
-
The class, and all instances of that class share that same variable and when changed by one instance, all the others will see that change.
-
You will see classes in action later this week.
-
Unlike variables, once set, the value of a constant is not meant to be changed.
API_BASE_URL = "http://pokeapi.co/api/v1/" def build_endpoint(path) API_BASE_URL + path end build_endpoint "pokemon/1/" => "http://pokeapi.co/api/v1/pokemon/1/"
-
What makes
API_BASE_URL
a constant? -
The scope of constants is based upon where they are declared:
- Globally
- Module
- Class
-
Before we do some practice, let's see how we can put Ruby code in a file and execute it in Terminal:
$ touch first_ruby.rb $ echo "puts 12345" > first_ruby.rb $ ruby first_ruby.rb $ 12345
-
Now you can open
first_ruby.rb
and write Ruby code for the following exercise...
-
Declare a constant that contains your name.
-
Declare a local variable that contains your age.
-
Use the
puts
method to print out a string that uses string interpolation and says:
"Hi there, my name is ________ and I'm ___ years old."
-
Is an empty string ("") truthy or falsey in Ruby? JavaScript?
-
In addition to global variables, what other 3 types of variables did we talk about?
#### Next up we're going to learn about control flow in Ruby, then more about its arrays & hashes.