Section 1

Preview this deck

else

Front

Star 0%
Star 0%
Star 0%
Star 0%
Star 0%

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Active users

0

All-time users

0

Favorites

0

Last updated

5 years ago

Date created

Mar 1, 2020

Cards (163)

Section 1

(50 cards)

else

Front

a statement that runs certain code only if the previous conditional statements do not run (must be paired with 'if' or 'unless' block and takes no arguments)

Back

#

Front

single line comment

Back

elsif

Front

a statement that runs certain code only if the previous conditional statements do not run and its boolean expression evaluates to true (must be paired with 'if' or 'unless' block and takes a boolean expression as it's argument

Back

next if

Front

keyword to skip over a certain step when condition is met

Back

!=

Front

comparator (or relational operator) meaning "not equal to"

Back

for loop

Front

definition: type of loop used to iterate an object when you know how many times to loop. The Ruby .each method is preferred over this loop however because it does not create a new scope for the object whereas the .each method does. This loop is rarely used

Back

break if

Front

keyword to stop a loop when condition is met

Back

control flow

Front

definition: select different outcomes depending on information the user types, the result of a computation, or the value returned by another part of the program

Back

.reverse

Front

method: backward version of character string

Back

&&

Front

logical/boolean operator "and" ~ only results in true when both expressions on either side are true

Back

.gsub!(/something/, "something else")

Front

syntax of global substitution method

Back

||

Front

logical/boolean operator "or" ("inclusive or") ~ results in true when one or the other or both expressions on either side are true

Back

do end

Front

keywords to wrap instructions of an iterator (generally interchangeable with curly brackets)

Back

i = 0 loop do i += 1 print "#{i}" break if i > 5 end

Front

syntax loop example: using keywords to increment and display "i" starting at 0, stopping if greater than 5 (example does not display each number on a newline)

Back

variable_name.method!

Front

(!) syntax for modifying (.method) the value contained within the variable (variable_name) i.e. subsequent uses of the variable will now inherit the change

Back

=

Front

assignment operator to set a variable

Back

.capitalize

Front

method: first letter CAPITAL rest of string lowercase

Back

!

Front

logical/boolean operator "not" ~ makes 'true' values 'false' and vice versa (prefix to value you wish to modify)

Back

gets (gets.chomp)

Front

method that gets input from the user (by default has line break added ...string with method to remove newline)

Back

while loop

Front

definition: type of loop that will execute a block of code as long as its condition is true. When the condition becomes false, the code after the end of the loop will be executed.

Back

?

Front

generally Ruby methods that end with this evaluate to the boolean values true or false

Back

expression

Front

definition: fancy word for something that has a value (like 4, true, or "pants")

Back

.include?

Front

method to check whether a value contains something; evaluates to true if it finds what its looking for or false if not

Back

.upcase

Front

method: convert character string to all UPPER CASE

Back

if

Front

a statement takes a boolean expression and executes certain code only if the boolean expression evaluates to true (manages a program's control flow)

Back

.chomp

Front

method: remove line break (newline)

Back

< <= > >=

Front

less than less than or equal to greater than greater than or equal to (example is separated by newline)

Back

.length

Front

method: number of characters in character string

Back

( )

Front

combine boolean operators with these. Expressions inside of these are always evaluated before anything outside of these

Back

for num in 1...10 puts num end

Front

syntax of loop, For the variable 'num' in the range 1 to 10 (excluding 10), display num on newline

Back

.gsub!

Front

method to replace every instance of 'something' it finds with 'something else' you specify (global substitution method) (example uses syntax to change value contained within the variable)

Back

#{variable_name}

Front

string interpolation syntax: call the value of variable_name

Back

=begin =end

Front

multiple line comment

Back

environment

Front

definition: the collection of all variables and their values that exist in the program at a given time

Back

print

Front

show on screen (prompt user)

Back

+= -= *= /=

Front

assignment operators to ADD SUBTRACT MULTIPLY DIVIDE a value to a counter, then assign that new value back to itself (example separated by newline)

Back

unless

Front

a statement that takes a boolean expression and executes certain code only if the boolean expression evaluates to false (manages a program's control flow)

Back

i = 0 while i < 5 puts i i = i + 1 end

Front

counter example where "i" increments from 0 to 4 using a while loop (displaying each number on newline) (uses "equals" assignment operator)

Back

.downcase

Front

method: convert character string to all lowercase

Back

puts

Front

show on screen with line break after it

Back

if (boolean_expression) (#do something) elsif (boolean_expression_2) (#do something different) else (#do something else) end

Front

syntax of if/elsif statement

Back

iterator

Front

definition: type of method that repeatedly invokes a block of code (like a loop)

Back

==

Front

comparator (or relational operator) meaning "is equal to"

Back

+ - * / ** %

Front

Operators: Addition Subtraction Multiplication Division Exponentiation Modulo (example is separated by a newline)

Back

variable_name =

Front

local variable syntax (lowercase / underscore spaced) "=" declares the variable

Back

i = 0 until i == 6 i += 1 end puts i

Front

until loop syntax where "i" increments from 0 until 6 and displays the final result on a new line (uses 'add' assignment operator)

Back

loop { }

Front

simple loop iterator syntax

Back

until loop

Front

definition: type of loop to execute a block of code as long as its condition is false. When the condition becomes true, the code after the end of the loop will be executed.

Back

"string".method.method2.method3

Front

method syntax (period initiates method on what it's attached to: can string multiple methods on same line)

Back

for num in 1..10 puts num end

Front

syntax of loop, For the variable 'num' in the range 1 to 10 (including 10), display num on newline

Back

Section 2

(50 cards)

.each { |x, y| }

Front

hash iterator method (curly bracket version): apply an expression to each element of an object key value pair on |x and y|, one at a time (used in place of loop { } iterator)

Back

case when (then) else end

Front

statement similar to if/elsif when; used when there are multiple conditions to check (4 mandatory keywords/1 optional in parenthesis; newline delimited shown in example)

Back

boolean ? expression if true : expression if false (ex: x<7 ? "yes" : "no")

Front

syntax of "ternary conditional expression"; one line evaluates a true/false statement, returns expression if true or expression if false

Back

x.times { } (or could use x.times do expression end )

Front

iterator method: perform a task on each item in an object a specified ("x" used in example) number of times

Back

short-circuit evaluation

Front

definition: Ruby doesn't look at both expressions on both sides of a boolean operator (&& or ||) unless it has to. (ex: if it sees "false && true" it stops reading as soon as it sees "&&" because it knows "false && anything" must be "false")

Back

.each_value

Front

method: iterate over just values

Back

my_hash = Hash.new

Front

hash new constructor method example: declare variable "my_hash" to a new empty hash; i.e. object (equal to empty curly braces {} ) (remember capitalization)

Back

<=>

Front

combined comparison operator: It returns 0 if the first operand (item to be compared) equals the second, 1 if first operand is greater than the second, and -1 if the first operand is less than the second.

Back

implicit return

Front

definition: Ruby's methods will return the result of the last evaluated expression (with or without writing return)

Back

hash

Front

definition: collection of key-value pairs

Back

.each {|variable| }

Front

iterator method (curly bracket version): apply an expression to each element of an object on |variable|, one at a time (used in place of loop { } iterator)

Back

[[0, 0, 0],[0, 0, 0]]

Front

multidimensional array example: 2 arrays of 3 zeros within an array

Back

.push

Front

method: add element to the end of an array. helpful to use when adding elements to an empty array variable. ex: empty_array.______(element to add)

Back

.to_i

Front

method: convert to integer

Back

my_hash = Hash.new([default_value])

Front

hash new constructor method syntax (variable declared as "my_hash"

Back

hash = { key1 => value1, key2 => value2, key3 => value3 }

Front

hash literal notation syntax example: showing 3 key-value pairs (key1, value1, etc)

Back

for i in 1..5 next if i % 2 == 0 print i end

Front

syntax loop example: for loop where "i" is in range 1 to 5 (including 5), skip where i modulo 2 is equal to 0 (skip even numbers), display results (no newline)

Back

expression unless boolean (no need for "end" keyword" (ex: "yes" unless x<0 )

Front

syntax of simplified "unless" statement; one line when expression is expecting a true or false result

Back

my_hash = Hash.new("Default Value") my_hash["key_1"] = "Default Value"

Front

hash new constructor method example: declare variable "my_hash" and fill empty hash with value (Default Value); set key of "Default Value" to "key_1" for my_hash using bracket notation

Back

next if i % 2 == 0

Front

syntax 'next if' example: skip odd numbers of "i"

Back

.split(" ")

Front

method: divide string by specified delimiter (single-space delimiter used in example)

Back

=>

Front

"hash rocket": characters used in hash literal notation syntax dividing keys and values

Back

.each_key

Front

method: iterate over just keys

Back

blocks

Front

definition: a way of creating methods that don't have a name (these are similar to anonymous functions in JavaScript); can be defined with either the keywords "do" and "end" or with curly braces ({})

Back

expression if boolean (no need for "end" keyword) (ex: "yes" if x<4 )

Front

syntax of simplified "if" statement; one line when expression is expecting a true or false result

Back

.sort!

Front

method: sort alphabetically or from smallest to largest (example uses syntax to change value contained within the variable)

Back

def

Front

keyword: define a method

Back

gets.chomp gets.chomp .each .delete

Front

CRUD: create, read, update, delete (example of each separated by newline)

Back

.to_sym | .intern

Front

methods: convert to symbol (2 options, separated by | in example)

Back

.select

Front

method: filter a hash for values that meet certain criteria

Back

.object_id

Front

method: gets the ID of an object—it's how Ruby knows whether two objects are the exact same object.

Back

next if i % 2 != 0

Front

syntax 'next if' example: skip even numbers of "i"

Back

.to_s

Front

method: convert to string

Back

.each do |x, y|

Front

hash iterator method (non-curly bracket version): apply an expression to each element of an object key value pair on |x and y|, one at a time (used in place of loop { } iterator)

Back

sounds = { :cat => "meow", :dog => "woof", }

Front

hash key symbol syntax example: variable name sounds has symbols for cat & dog; with values of meow & woof respectively (example shown using hash literal notation)

Back

: (followed by _ or letter, no spaces)

Front

symbols always start with this, in new hash literal notation (new 1.9 hash syntax), this character may be placed at the end of the symbol

Back

movies[title.to_sym]=rating.to_i

Front

updating hash key value pair example: where title=gets.chomp & rating=gets.chomp update movies hash with key value pair title:rating where title is a symbol and rating is an integer

Back

frequencies = frequencies.sort_by { |word, frequency| frequency}

Front

key value pair sort by example: where frequencies (variable name) is replaced by sorted by array of arrays. key is called "word", value is called "frequency".

Back

||=

Front

conditional assignment operator: assign a value to a variable if it hasn't already been assigned (nil) ~ otherwise leave existing value intact. Note 'nil' is not the same as false or empty {}

Back

.delete

Front

method: remove from variable

Back

method

Front

definition: a reusable section of code written to perform a specific task in a program (separation of concerns)

Back

hash = { key1: value1, key2: value2, key3: value3 }

Front

new 1.9 hash literal notation syntax example: showing 3 key-value pairs (key1, value1, etc) ... note the "symbol" notation at the end of the key and degradation of "hash rocket"

Back

s.each { |sub_array| }.each{ |items| puts items} (or s.each do | sub_array | sub_array.each do | items | puts items end end )

Front

iterating over multidimensional array example: Puts out every element inside the sub-arrays inside s. Iterate through .each element in the s array. Call the elements sub_array. Then iterate through .each sub_array and puts out their items.

Back

.each do |variable|

Front

iterator method (non-curly bracket version): apply an expression to each element of an object on |variable|, one at a time (used in place of loop { } iterator)

Back

my_hash.delete(key)

Front

delete key value pair from hash example: where my_hash is the hash name, key is the key name and value is the value name

Back

books.sort! { |firstBook, secondBook| secondBook <=> firstBook }

Front

example syntax sort block: for variable books, using arguments |firstBook, secondBook| sort in descending order

Back

return

Front

store a value from a method using this (rather than just printing to console for instance). Can be used by another method

Back

["x", "y", 1, 2, true, false]

Front

array syntax (example: x, y, 1, 2, true, false)

Back

array[2]

Front

array 'access by index' example: find 3rd value of array = [22, 33, 44] (remember what number indexes start with)

Back

*

Front

splat arguments are arguments preceded by this ___<by what?> , which signals to Ruby: "Hey Ruby, I don't know how many arguments there are about to be, but it could be more than one."

Back

Section 3

(50 cards)

super

Front

keyword to access method or attribute of a parent or superclass that has already been overwritten. When you call THIS from inside a method, it tells Ruby to look in the superclass of the current class and find a method with the same name as the one from which THIS was called. If it finds it, Ruby will use the superclass' version of the method.

Back

Proc

Front

definition: a "saved" block: just like you can give a bit of code a name and turn it into a method, you can name a block and turn it into a _________

Back

mixins

Front

Definition: THESE incorporate data or behavior from several classes into a single class.

Back

array.method(&proc)

Front

example syntax for calling a proc named "proc", using a method called "method" over each value of an array named "array". Careful to replace blocks' normal wrapper {} and to delete any spaces between the method and the new wrapper

Back

.collect {} | .map {}

Front

method(s) that takes a block and applies the expression in the block to every element in an array

Back

attr_writer :variable

Front

Rather than def a method to write to a variable within a class, we can now use this instead to call the variable in order for it to be written to

Back

private

Front

Keyword to make methods private (above the def/end statement)

Back

@

Front

Symbol: prefix of instance variable

Back

lambda { }

Front

keyword that replaces Proc.new { }

Back

array.collect(&:to_s)

Front

syntax example: collect each integer element in an array ("array") to a string (interchangeable with .map)

Back

ALL_CAPS =

Front

Syntax for constants (example uses the words "all caps")

Back

block

Front

Definition review: THIS is just a bit of code between do..end or {}. It's not an object on its own, but it can be passed to methods like .each or .select.

Back

lambda

Front

Definition review: THIS is just like a proc, only it cares about the number of arguments it gets and it returns to its calling method rather than returning immediately.

Back

module

Front

Definition: a toolbox that contains a set methods and constants. They're very much like classes, only they can't create instances and can't have subclasses. They're just used to store things!

Back

.downto( ) {|x| }

Front

method: iterates a block and passes descending range down to and including the limit set. Like a counter that gives back the range between two characters (either letters or numbers).

Back

public

Front

Keyword to explicitly make methods public (above the def/end statement)... methods are public by default

Back

class

Front

Definition: THIS is just a way of organizing and producing objects with similar attributes and methods

Back

Proc.new { }

Front

define a "proc"

Back

.respond_to? ( : (colon replaces period of method in question, i.e. .push becomes :push, .to_sym becomes :to_sym)

Front

method: check to see if a method will respond to a given object

Back

global variables local variables class variables instance variables

Front

Definition: When dealing with classes, you can have variables that are available everywhere. These are called _____________. Those that are only available on certain methods: _____________. Others that are only available to certain classes ______________ ; and lastly those that are available to certain instances of classes ...called _____________.

Back

lambda { |param| block }

Front

lambda syntax where "param" is the parameter and "block" is where the expression goes

Back

<<

Front

concatenation operator that replaces .push for adding an element to the end of an array. Also known as "the shovel"

Back

1) a lambda checks the number of arguments passed to it, while a proc does not (lambda will throw an error if you pass it the wrong number of arguments, whereas a proc will ignore unexpected arguments and assign nil to any that are missing) 2) when a lambda returns, it passes control back to the calling method; when a proc returns, it does so immediately, without going back to the calling method. (so additional steps on proc may get overwritten by the proc whereas the lambda itself may get overwritten by further steps in the method)

Front

2 differences between proc and lambda <example shown with bullets 1) 2) >

Back

override

Front

Definition: sometimes you'll want one class that inherits from another to not only take on the methods and attributes of its parent, but to ___________ one or more of them. This new version will ____________ (that is, replace) the inherited version for any object that is an instance of the DerivedClass.

Back

proc

Front

Definition review: THIS is a saved block we can use over and over.

Back

My name is Kim My name is Eric Your name is Kim Your name is Steve

Front

yield with parameters example: def yield_name(name) yield("Kim") yield(name) end yield_name("Eric") { |n| puts "My name is #{n}" } yield_name("Steve") { |x| puts "Your name is #{x}" } (what will results print like?)

Back

yield with parameters

Front

description: if variable has an argument placeholder like variable(name) then yield also takes the same argument placeholder like yield(name) or a value like yield("steve"). The yield callout (below the end of the method) in that case needs a yield block to tell how to handle the passed parameter

Back

@@

Front

Symbol: prefix of class variable

Back

attr_reader :variable

Front

Rather than def a method to access a variable within a class, we can now use this instead to call a variable for it to be accessed by the class

Back

@

Front

Symbol used before a variable to signify that it's an instance variable (prefix). This means that the variable is attached to the instance of the class.

Back

&

Front

symbol used to convert the cube proc into a block (prefixed to variable name given when proc was created)

Back

constants

Front

It doesn't make sense to include variables in modules, since variables (by definition) change (or vary). THESE however, are supposed to always stay the same, so including them in modules is a great idea.

Back

::

Front

Symbol used for "Scope Resolution Operator" which tells Ruby where you're looking for a specific bit of code. Ex: look inside the Math module to get the PI constant.

Back

def variable puts "abc" yield puts "123" end variable { puts "this goes between abc 123" }

Front

simple yield example; variable uses curly brackets to insert saved block expression back into the method at the point of the yield keyword

Back

multiple inheritance

Front

Definition: some languages allow a class to have more than one parent, which is a model called _________________. Ruby doesn't allow this; any given Ruby class can have only 1 superclass.

Back

module ModuleName end

Front

Syntax of module definition; note: CapitalizedCamelCase

Back

array.map(&:to_i)

Front

syntax example: map each string element in an array ("array") to an integer (interchangeable with .collect)

Back

$

Front

Symbol: prefix of global variable when used inside of a method or class. When outside a method or class no symbol is needed.

Back

namespacing

Front

Definition: One of the main purposes of modules is to separate methods and constants into named spaces. This is called ____________

Back

#{}

Front

string interpolation syntax: insert value of a variable directly into a string without having to + or << or convert integer .to_s (for example)

Back

I'm the method! I'm the lambda!

Front

example lambda syntax: def lambda_demo(a_lambda) puts "I'm the method!" a_lambda.call end lambda_demo(lambda { puts "I'm the lambda!" }) (what will it print to console?)

Back

attr_accessor :variable

Front

Rather than def a method to read & def a method to write to a variable within a class, we can now use this to do both

Back

.call

Front

method for calling a proc (alternative to (&proc) method)

Back

.upto( ) {|x| }

Front

method: iterates a block and passes ascending range up to and including the limit set. Like a counter that gives back the range between two characters (either letters or numbers).

Back

class NewClass end

Front

class syntax example: using NewClass as the class name (remember to use CamelCase with 1st letter cap)

Back

yield

Front

methods that accept blocks have a way of transferring control from the calling method to the block and back again. We can build this into the methods we define by using the ________ keyword

Back

global methods class methods instance methods

Front

Definition: When dealing with methods, you can have methods that are available everywhere, those that are available only to those of a certain class, and others that are only available to particular instance objects. These methods are called: ________, __________, & ___________

Back

class DerivedClass < BaseClass (# Some stuff!) end

Front

Syntax of inherited classes where DerivedClass inherits from BaseClass

Back

.next

Front

method: will return the integer immediately following the integer it's called on, meaning 4.____ will return 5

Back

object-oriented programming language

Front

Definition: THIS language type manipulates programming constructs called objects. Objects have methods and attributes.

Back

Section 4

(13 cards)

yield accepts arguments example

Front

def who_says_what yield("Dave", "hello") yield("Andy", "goodbye") end who_says_what {|person, phrase| puts "#{person} says #{phrase}"}

Back

Two examples of code blocks

Front

{ puts "Hello" } .........and......... do club.enroll(person) person.socialize end

Back

require

Front

Keyword used to explicitly bring in a module to the interpreter which has not yet been brought in

Back

include Date

Front

Syntax for including a module (named Date in example) (notice case sensitivity in answer)

Back

statement modifiers, if statement

Front

regular if statement: if radiation > 3000 puts "danger" end ...............instead.............. puts "danger" if radiation > 3000

Back

include

Front

Keyword: used to explicitly bring in the methods and constants of a module where you no longer have to prepend your constants and methods with the module name (as is the case with 'require'). mixes a module's methods in at the instance level (allowing instances of a particular class to use the methods)

Back

extend

Front

Keyword: mixes a module's methods at the class level. This means that class itself can use the methods, as opposed to instances of the class.

Back

code block using yield

Front

def call_block puts "Start of method" yield yield puts "End of method" end call_block { puts "In the block" }

Back

def initialize(name, balance=100)

Front

Syntax example for providing an optional parameter with a default value in the initialize statement. ex: define initialize with two parameters: name & balance. Set balance to 100 if argument is not defined.

Back

iterator code block

Front

animals = %w( ant bee cat dog) animals.each {|animal| puts animal }

Back

statement modifiers, while loop

Front

regular while statement: square = 4 while square < 1000 square = square*square end ..............instead............... square = 4 square = square*square while square < 1000

Back

loops as method calls

Front

[ 'cat', 'dog', 'horse' ].each {|name|print name, " " } 5.times { print "*" } 3.upto(6) {|i| print i } ('a'..'e').each {|char| print char } cat dog horse ***3456abcde

Back

require 'date'

Front

Syntax for requiring a module that needs to be brought into the interpreter. Bring in the Date module. (notice case sensitivity in answer)

Back