Basic Ruby Terms for Beginners
A cheat sheet for new developers
--
When I started to learn Ruby, even the English descriptions seemed like they were written in another language. Here are quick definitions for terms I looked up over and over again as a beginner, with the basic information I needed to get unstuck.
The “I” Guys
Immutable — an immutable (unchangeable) object’s state cannot be modified after it is created. More on immutable objects.
Incrementing — where an integer increases by one with each loop, for example. More here on loops.
Initialize — to create data in a class that will apply to each instance of the class. A class is like a template for creating objects in Ruby.
Instantiate — create a new instance of a class
Integer — a number (whole number, not a fraction)
Interpolation — to embed a variable in a string. Example: “I love my #{dog}.”
Invoke/invocation — to call a method (make it run)
Iterate — you “iterate through” a hash or array. To do something to each object in a collection.
Enumerator — (honorary “I guy”) a tool for sorting items in a collection. More on enumerators here.
Arrays
Arrays in Ruby can contain any data types in any combination — strings, integers, other arrays, hashes.
Access an item — famous_cats[1] #=> “Maru”
Reassign value to an index — speed_dial[1] = “Cheetos”
.unshift — add an element to the front of an array: famous_cats.unshift(“Maru”)
.pop — remove last item from array. It also provides the removed element as its return value: famous_cats.pop
.shift — remove first item: famous_cats.shift
Add items to an array:
Shovel — add only one element: famous_cats << “Maru”
Push— add multiple elements: speed_dial.push(“Maru”, “Jennifer”)
An index of -1 is the last element of the array, -2 is the next to last, etc.
Booleans
Booleans — a value that is either true or false.
Truthy/falsey — the state of being true or false. “If a statement evaluates to true (is truthy), then run this code.”
Falsey — in Ruby, only false and nil. Everything else is truthy (even 0).
TrueClass/FalseClass — every instance of true/false in a Ruby program
! — single bang operator, negates the boolean value that it’s in front of:
!true #=> false
!! — double-bang operator, returns true or false based on whether a value is truthy or falsey to begin with: !!”hello” #=> true, !!nil #=> false
&& — AND
true && true #=> true — to evaluate to true, both values, on either side of &&, must evaluate to true
true && false #=> false
|| (“double-pipe”) — OR
For || to evaluate to true, only one value on either side of it must evaluate true: false || true #=> true
== — comparison operator — if the two values being compared are equal, the statement returns true
= — assignment operator — sets a variable equal to a value (assigns value)
Comparing numbers — example: 14 > 3 #=> true — 14 is larger than 3, so Ruby evaluates this to true.
Comparing strings — example: “yellow” == “yellow” #=>true
Comparing variables with known values:
my_mood = “happy”
my_mood == “happy” #=> true
Ruby operators cheat list
More here at RubyGuides.
== If the values of the two operands are equal, the evaluation is true.
!= If the operands are not equal, the evaluation is true.
> If the left operand is greater than the right operand, the evaluation is true.
< If the left operand is less than the right operand, the evaluation is true.
>= If the left operand is greater than or equal to the right, the evaluation is true.
<= If the left operand is less than or equal to the right, the evaluation is true.
General
Conditionals — forms (if, else, and elsif statements, case statements, loops) to execute certain code if that code’s boolean condition evaluates to true or false. This whole kit and caboodle is “conditional logic.”
Data structures — in general, arrays and hashes. In a sentence: “A Tic Tac Toe program requires data structures for storing the board and conditional logic for knowing whose turn it is.”
Decomposition — “Factoring,” breaking down a complex problem or system into parts that are easier to conceive, understand, program and maintain.
Default argument — for example, if you don’t supply a name, this program will supply “Ruby Programmer” as the default name:
def say_hello(name = “Ruby Programmer”)
puts “Hello #{name}!”
end
Gems — RubyGems is a package manager (pm), the standard format for distributing Ruby programs and libraries. Gems are tools you can include and use in your own code.
Helper methods — Methods you write (usually to take care of routine tasks) to use inside other methods.
Line (a new line, like a hard return) — “\n”
Loops — tell a program to do something a certain number of times, or until a condition is met: if, while, until. You can stop loops with break and counters. Iterators (like each) are called within blocks and perform more complex operations on a collection. Those don’t need counters.
Methods
To define new routines and procedures for code. It’s what code does.
It’s like building “a little machine that says hello three times.”
“If variables are nouns, methods are verbs. If variables encapsulate and abstract data, methods encapsulate and abstract procedure,” according to the Flatiron School.
You “invoke” or “call the method” by its name. You call it on whatever it will be operating on.
Syntax convention — preface methods with # (for example: #greeting) so developers know it’s a method, not a variable, bareword or class.
Source: this lesson on methods from Flatiron School.
Object — a bundle of info and behaviors. For example, a string is an object because it contains info and has behaviors. It can do things and have things done to it.
Programming language library — “Collection of precompiled routines a program can use. The routines, sometimes called modules, are stored in object format. Libraries are useful because you don’t need to explicitly link frequently used routines to every program that uses them. The (L) linker automatically looks in libraries for routines it doesn’t find elsewhere.” Source: Vangie Beal, Webopedia
Ruby on Rails — popular framework for web applications.
Web application framework — provide packages that handle the nitty-gritty details of the HTTP protocol. At their core they listen for requests and send HTTP responses with some HTML back. Source: Jeff Knup, developer
Rakefile — “Rake, short for “Ruby make” is a native tool for Ruby, similar to Unix’s “make.” It handles administrative commands or tasks, stored either in a Rakefile or in a .rake file. You can write your own rake tasks, specific to an application, and there are rake tasks natively built into Ruby and Rails for certain functions.” Source: Amber Wilkie, “What the Heck is Rake?”
Recursion — to call a method from within itself
Validation — for example, #position_taken? in a tic tac toe game checks if the user’s submitted position is free. It guards the game from breaking when the user submits a position that isn’t available.
Variables
Variables are versatile, and can point to almost any type of value including numbers, strings, arrays and hashes. Example: these_things = 1+1
(Arrays are the other virtuoso, storing any type of data.)
Write variables in snake case: this_is_an_example_of_snake_case. They should start with a lowercase letter, since constants are uppercase.
A variable’s type is the type of the value it holds.
Ruby is a dynamically typed language — the value can change its type and does not need to be explicitly and permanently defined.
Source: Flatiron School
Symbols
Generally used to identify a specific resource. A resource can be: a method, a variable, hash key, a state etc. A symbol is uniq: only one instance of the Symbol class can be created for a specific symbol in a running program:
:pending.object_id # => 1277788
:pending.object_id # => 1277788
Symbols are often compared to strings, but the main difference = a new String object is created for each called string — even if they’re identical
‘pending’.object_id # => 70324176174080
‘pending’.object_id # => 70324176168090
Create it by typing it; “a Symbol object is implicitly instantiated when a new symbol is declared.” Source: RubyCademy, “Symbol in Ruby.”
Appendix
Programs are composed of basically three things
Anything else will cause an error.
1. Keywords — (approx. 43 of them) — specific tasks
2. Strings — literal pieces of data like this sentence, also numbers
3. Barewords — you define and create them: variables, methods (also, method parameters and parameterless methods) set equal to things
Source: Flatiron School
Six Ruby data types (classes)
- Strings
- Booleans
- Numbers/integers
- Arrays
- Hashes
- Symbols
Software engineer vs web developer
Software engineers: build (engineer) software, desktop programs like Spotify or Photoshop, or mobile and web applications (like Facebook, Twitter, or Gmail). A web developer is also a software engineer, but primarily builds web applications.
UX vs UI design
UX — everything from physical products to digital, any interaction a customer has with the product, market research, wire frames and prototypes, analytical, functional, useful, psychology.
UI — digital — visual screens and touchpoints the user interacts with. Focuses on visual, interactive experience. Every screen, button, the flow, color, typography, animation, consistency (create style guide), mock ups and prototypes. Related to graphic design. Source: Interaction Design Foundation.
Let me know what I made more confusing! Connect here or on Twitter @SaraHarvy.