Section 1

Preview this deck

Until loops

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

4 years ago

Date created

Mar 14, 2020

Cards (39)

Section 1

(39 cards)

Until loops

Front

Ruby includes an until loop that will 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. until condition_is_false # do something end

Back

Division

Front

If you divide two integers, the outcome will be an integer regardless of remainder (see modulus below for more information). However, if you divide an integer with a floating point number or two floating point numbers, the outcome will account for the remainder. 10 / 3 => 3 10.0 / 3 => 3.33333333333333 10 / 3.0 => 3.33333333333333 10.0 / 3.0 => 3.33333333333333

Back

Unless

Front

The statement takes a boolean expression and executes certain code only if the boolean expression evaluates to false. unless boolean_expression #do something here end

Back

Hashes

Front

Hashes are collections of key-value pairs. Like arrays, they have values associated with indices, but in the case of hashes, the indices are called "keys."

Back

Array.each

Front

This is very similar to a regular array, however, it can also be used to run a block. Put a do after the array.each and insert the object in the parenthesis or absolute value sign array.each do |arg|

Back

Ternary operators

Front

This is a shorthand statement for a simple if...else statement. It is a useful tool in situations where you have an extremely simple if...else statement where you are trying to assign a value to a variable. boolean_expression ? true_expression : false_expression

Back

Basic arithmetic

Front

Your typical addition, subtraction, multiplication, division, and exponentiation all exist in Ruby and look very similar to your typical use cases in algebra. Note that order of operations holds. x + y #addition x - y #subtraction x * y #multiplication x / y #division x ** y #exponentiation

Back

Switch statement

Front

Acts like a big if / else if / else chain. Checks a value against a list of cases, and executes the first case that is true. If it does not find a match, it attempts the default case designated by an else statement. If there is not a default case, then it exits the statement. Unlike languages like JavaScript, Ruby switch statements have no fall through and automatically break. Instead, cases can be comma delimited. case value when expression1 #do something when expression2 #do something ... when expressionN #do something else #do default case end

Back

Comparison operators

Front

Comparison operators are used to test the relationship between two objects. The equality (==) and inequality (!=) operators can be used on almost any type of value where the other operators are used for numeric comparisons.

Back

Modulus division

Front

If you would like the remainder from an integer division problem, you would use the modulus operator to retrieve the value. The modulus operator is the % sign. 10 % 3 => 1

Back

.floor

Front

Returns the largest integer less than or equal to a number. Example: 9.99.floor => 9

Back

Arrays

Front

a set of data that contains one or more values

Back

Single-line

Front

Text is typed next to a # sign # This is a single line comment.

Back

.ceil

Front

Returns the smallest integer greater than or equal to a number. 45.4.ceil => 46

Back

Array.flatten

Front

.flatten returns a one-dimensional version of a multi-dimensional array. It does not overwrite the array with the new array. [[1,2,3], [4,5,6], 7, [[8,9], 10]].flatten => [1,2,3,4,5,6,7,8,9,10] multiple arguments are inputted in brackets and then .flatten is typed in.

Back

Methods

Front

A Ruby method is used to create parameterized, reusable code. Ruby methods can be created using the syntax: def method_name(arguments) # Code to be executed end

Back

.divmond

Front

If you want to do integer division and retrieve both the quotient and the remainder in one call, then you want to use divmod. You can use divmod on a numeric type and it will return an array with the quotient and the remainder, respectively. 10.divmod(3) => [3, 1]

Back

.each

Front

.each is a built in iterator function in Ruby. It loops through each item in a list, hash, or other iterable object allowing you to perform operations on that value. The block of an .each statement creates a new scope for your variable so you don't accidentally modify the original value. iterable_obj.each do |value_of_item| # do something end

Back

Booleans

Front

two values : true, false Conditional statements are created and the programs returns a true or false answer

Back

.sqrt

Front

Returns the square root of a number. Math.sqrt(expression)

Back

If

Front

used to start a conditional statement if boolean_expression #do something here end

Back

Pi

Front

Returns the ratio of the circumference of a circle to its diameter, approximately 3.14159 or in better terms, the value of PI (π). Note in syntax , we do not put () at the end of Math::PI because Math::PI is not a function but rather a constant. Math::PI => 3.14159265358979

Back

Comments

Front

human readable comments

Back

puts vs. print

Front

The puts (short for "put string") and print commands are both used to display the results of evaluating Ruby code. The primary difference between them is that puts adds a newline after executing, and print does not. print some_string puts some_string

Back

For loops

Front

The for loop is used to iterate an object. The Ruby .each method is preferred over the for loop because the for loop does not create a new scope for the object whereas the .each method does. The for loop is rare in Ruby. for iterator in iterable_object # do something end

Back

Creating arrays

Front

# Array.new constructor variable = Array.new([repeat], [item]) An array is used by make an Array.new() that carries an object in the parenthesis.

Back

Elsif

Front

A conditional statement used to manage a program's control flow. The statement must be paired with an if or unless block and takes a boolean expression. It runs certain code only if the previous conditional statements do not run and its boolean expression evaluates to true. it is equivalent to writing an else statement that has an if statement in its block. if boolean_expression #do something elsif boolean_expression_2 #do something different else #do something else a combination between the else and if statement

Back

Blocks

Front

A block is a chunk of code that lives inside a control statement, loop, method definition, or method call. the braces from the objs.method { |obj| do_something } for multiple lines use the do/end function objs.method do |obj|

Back

Symbols

Front

In Ruby, a symbol is simply a name used in your program. One of the main uses for Ruby symbols is hash keys, especially if you would otherwise use the same string as a hash key over and over. Ruby will create an (almost) unlimited number of string instances for all your hash keys, but will only keep one copy of a symbol in memory at a time. This can really save memory for your programs in the long run. Syntax: :symbol

Back

While loops

Front

Ruby includes a while 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. while condition_is_true # do something end

Back

Creating shorthand hashes

Front

Rather than specifying a symbol then using the hash rockets to define key value pairs, you can now just put the key followed by a colon then the value. The keys get translated into symbols. my_hash = { key1: value1, key2: value2 }

Back

Multi-line

Front

=begin comment line comment line =end

Back

Creating standard hashes

Front

# Hash.new constructor my_hash = Hash.new([default_value]) You can use the new hash notation or the literal notation as seen below: # Hash literal notation my_hash = { "key1" => value1, :key2 => value2, 3 => value 3 }

Back

.times

Front

.times is a built in iterator function in Ruby. It performs an action a given number of times. num_of_times.times do # do something end

Back

Logical operators

Front

Logical operators are used to compare to boolean values. Ruby has 6 operators to compare boolean values: and, or, not, &&, ||, and not. and and &&, or and ||, and not and ! have the same functionality but the verbiage operators (and, or and not) have lower precedence than the symbolic operators (&&, || and !). !true => false not false => true

Back

Array.uniq

Front

You can remove duplicates from an array using Array.uniq. [1,1,1,2,3,4,3,3].uniq => [1,2,3,4] Simply place the arguments in brackets and type Array.uniq next to the brakets.

Back

Strings

Front

Strings are used for storing and manipulating text in Ruby. Strings are written between quotation marks. Both single (') and double (") quotes are supported, but quotes at each end of a single string must match (no "strings' or 'strings")! single_quotes = 'some text goes here' double_quotes = "some text goes here"

Back

Variables

Front

Variables are assigned values using the = operator, which is not to be confused with the == sign used for testing equality. A variable can hold almost any type of value including numbers, strings, arrays, and hashes. variable_name = value

Back

Else

Front

A conditional statement used to manage a program's control flow. The statement must be paired with an if or unless block and takes no arguments. It runs certain code only if the previous conditional statements do not run. if boolean_expression #do something else #do something else end

Back