Intro to Ruby
Objectives
Describe the history of the Ruby language
Identify fundamentals and concepts of the Ruby langauge
Utilize different primitive types, control structures, and methods in Ruby
Humble Origins
Ruby is an object-oriented language suitable for writing day to day scripts as well as full-scale applications. Yukihiro Matsumoto, or "Matz", began work on Ruby back in 1993, because he wanted a language that made him productive while being fun to use. Initially popular in Japan, Ruby has been finding its way into the hearts of programmers all over the world.
Ruby stylistically conforms to the snake_case convention
The documentation is fantastic
Further reading: The Philosophy of Ruby
Yukihiro Matsumoto
Why JavaScript, then Ruby?
While Ruby is a general purpose language that can be used for many purposes, we'll be applying it to a web development framework called Rails. We learned JavaScript first because it's the only language that runs natively in browsers, and we'll be utilizing some JavaScript for our front-end code, while utilizing Ruby for our back-end code.
You'll also find that while Ruby is a functional language, functions cannot be passed into other functions (functions are not first-class citizens). However, its object-oriented capabilities and clean syntax provide different strengths as a language. The widely used Rails framework also provides an opinionated development workflow, which can lead to faster development.
Comments
In JS, we use line and multiline comments.
In Ruby, multiline comments exist, but we generally use line comments with hashtags, for readability.
Variables
Local variables start with a lowercase letter. No var
necessary.
Constants
Mostly, we're able to change what a variable's holding if we so choose – constants are designed for the opposite. Constants are meant to be placeholders that never change.
Note that if we try to reassign a constant, the reassignment still succeeds! All the constant syntax does is throw an error on reassignment.
Data Types
Nothingness
Just as Javascript uses undefined or null, ruby uses nil
Booleans
A binary representation: either true
or false
Numbers
Datatypes used to represent a number
Fixnum:
23
Bignum:
23238923859348534535
Float:
23.23
Strings
A primative datatype used to represent a string of characters
Methods
Examples
Operators
Note that Ruby has a ===
operator, but no !==
operator. In fact, the operator means something different in Ruby. We'll touch on this when we get to ranges. You can use the .equal?
function as an identity operator.
Arrays
An indexed arrangement of objects
several ways to create an array
Array Methods
Examples
Ranges
A set of values with a beginning and an end
typecasting in action
Using === to determine if an element is within a range or set
Symbols
An immutable sequence of characters that represents data stored in a specific memory location. Symbols optimize memory and can help programs run faster when performing comparisons or lookups.
Hashes
A hash consists of unindexed key-value pairs. You may construct a hash in either of the following ways. Each will use symbols.
Mutator methods !
Mutator methods will not just return a value, but change the object they are called on to that value. Adding ! to certain ruby methods will turn them into their mutator method counterparts.
How to mutate an array
Typecasting
Typecasting is the act of altering an object's datatype
Code blocks
Sometimes called closures in other languages is a chunk of contained code. Use curly braces, { }
for single line blocks and do ... end
for multiline blocks.
String Interpolation
Allows one to inlcude a dynamic variable in a string. String interpolation can only be done on double-quoted strings.
Control flow
Conditionals
Loops
Enumerables (similar to iterators)
Examples
If/Else
Inline conditional
Loops
Iterating through Arrays
Enumerables
Iterating through Hashes
Functions
In Javascript
anonymous:
function (param1, [..param2, [...]]){...}
,named:
function Name(param1, [..param2, [...]]){...}
uses lexical scope
used as values (functional programming)
require explicit return
all
params
are optional
In Ruby
uses
def
does not capture scope
not used as values
implicitly returns last evaluation
optional parameters must be specified
Examples
In Ruby, leaving the ()
off of a function call is acceptable. Since functions can't be passed as values (i.e., aren't first-class), Ruby knows that we mean to call the function, so it calls it.
Parameters (Arguments)
Return Values
Ruby will automatically return the value of the last evaluated expression. This is called having "implicit returns". You are free to have an explicit return statement, but you don't have to.
Input / Output
You've already seen how puts
will output information to the screen. What if we want to accept user input? Let's try gets
.
That almost works as we want, but gets
is reading in the newline character from when we pressed the Enter key. Generally, when reading user input we want to chomp
the data. (See http://www.ruby-doc.org/docs/Tutorial/part_02/user_input.html)
Much better. Now the unnecessary newlines are removed, thanks to chomp
.
Last updated