all Flashcards
what is the ruby equivalent of console.log
puts
how do you comment out in Ruby
#
do you run Ruby in the browser
no
how do you run ruby application
ruby filename.rb
what is the difference between puts and print
puts adds a line break and print does not
to log an array what do you use
P
P[1,2,3]
what do you use to log complex data like nested arrays and hashes
PP “pretty printing’
abbreviation for “Interactive Ruby”
irb
what does irb do
runs Ruby REPL in the terminal similar to browser console
what does REPL stand for
READ
EVALUATE
Print
Loop
what does irb return
2 lines
output of code
return value
what do you input in irb
code you want the output and return value for
not all methods have a return value
false
nil means
no value (like null)
what is snake case
how you write in Ruby using a _ between words instead of camel case
how do you exit irb
ctrl + d
what are the 3 parts of an error message
- location of error (where)
- description (why)
- type of error (who)
4 common types of errors
- name errors
- syntax error
- type error
- division error
8 common data types in Ruby
strings
methods
numbers
nil
boolean
symbols
arrays
hashes
what are strings
words
how to interpolation in Ruby
double quotes “ “
what are methods
like functions that get called on an instance or class
what is an instance
one unique object
What do you put at the beginning of instance methods
#
what do you put at the beginning of class methods
Self
what are the 2 number types
integers- whole numbers (7)
floats- decimal numbers (7.2)
how do you convert data types to integers
_i
how do you convert data types to floats
_f
what does nil represent
the absence of value
can you assign nil as a value
yes
what are the types of falsey values
nil and false
what do symbols represent
a piece of data
how do you assign a symbol
:symbol_name
how to create array
array_name.new
what are hashes
same as objects
syntax for making methods
def method_name
#code
end
you must use paratheses
false
what is the return value of a method
the last line of code before end
what is scope
areas in a program where certain data is available to the programer
local varaible created inside a method are unavailable outside the method
true
local varaible created outside a method are unavailable inside the method
true
how do you pass varaible outside of method
as an arguement
4 variable types
local - start with lower case letter
global- start with $
instance - start with @
class - start with @@
where does global give access
to everywhere in code
you should use global often
false
what is pry
Ruby REPL with added functionality from irb
what is binding.pry similar to in JS
debugger
what does binding.pry add to code
a breakpoint in code to pause execution to check variables, methods and other context
what must you add to the top of application to use pry
require ‘pry’
how do you leave pry console
exit
what is the else if syntax in Ruby
elsif
syntax for unless
unless condition
if condition is false run the code here
end
what is statement modifier
write conditions at the end of a line
this_year = 2025
puts “Hey, its 2026” if this_year == 2026
what does case statements fo
run multiple conditions against one value
syntax for case statements
case condition
when “value”
“do this”
when “other value”
syntax for case statements
case condition
when “value”
“do this”
when “other value”
“do this”
else
“do this if no value matches condition”
end
what does while loops allow us to do
run same line of code multiple times
while syntax
i = 0
while i<5
puts “count”
count += 1
end
until loop do
will run until a condition is met
until loop syntax
count = 0
until counter == 10
puts “count”
counter += 1
end
time loops are called on what
a number
syntax for time loops
10.times do |i|
puts “loop”
puts “i is: #{i}”
end
(each loop updates i )
what does each loop get used on
objects and array
each loop syntax
(1.. 20).each do |num|
puts num
end
what do arrays do
store list of data in a specific order
CRUD stand for
Create, Read, Update, Delete
what ruby methed gets the first element of array
array.first
what ruby method gets the last element of array
array.last
what ruby method gets the number of elements of array
array.length and array.size
what ruby method gets the range of elements of array
array[0..1]
what is the difference between 2 dots and 3 dots in the array bracket range
2 dots gives all numbers including first and last
3 dots give all numbers except the last
what ruby method gets access to multiple elemets of array
array.slice(0,2)
what ruby method adds an element to the end of array
array.push
what ruby method adds an element to the frony of array
array.unshift (new) and array «< new element
what is shovel method
«< that adds to an array
array «< new element
what ruby method adds two arrays together and changes the first array
array1.concat(array2)
this changes array 1 to include all of the elements
what ruby method adds two arrays together and changes doesnt change the first array
array1 +array2
what ruby method removes the last element
array.pop
what ruby method removes the first element
array.shift
what ruby method finds if an element is in an array
array.include?(element)
what ruby method reverse all elements and doesnt change original array
array.reverse
what ruby method reverse all elements and does change original array
array.reverse!
what rby method adds every element together
array.sum
what ruby method returns the unique elements
array.uniq
how to create a hash
{key1: “value 1” , key2: “value 2”}
how to access key within a hash
bracket notation [:key1]
how do you associate a new value to a key
bracket notation hash_name[:key1] = “new value”
you can use dot notation with hash
false
hash method that delete a key and value pair
hash.delete(:key)
hash method that returns all keys
hash.keys
hash method that returns all values
hash.values
has method that determines if hash is empty
hash.empty
hash method to join 2 together
hash1.merge(hash2)
what is enumberables
iterating over every piece of data
iterate every element of an array and return a new array by transforming the values to new values
.map
syntax for .mao
new_array = array.map do |block parameter|
block parameter. method to change each element
end
what is block parameter in .map
represents each element in array
what is the block
chunk of code between do and end keywords
what are pipes ||
argument that is being passed into the block
how do you map using the index
.map.with_index
what do you use to enumerate hashes
.each
what do you use .each for it
want to access each element of array but don’t care about returning a new array
what do you use .map / .collect to do
want to access every element in an array, calculate a new value and return a new array with the same length as original array
what do you use filter/select/find_all to do
want to access every element check if it matches some criteria and return a new array of all the values that match
what do you use find/detect for
want to access every element of array, check if it matches some criteria and return first element that matches only
what do you use sort for
retunr a new array where all the elements have been sorted by some condition
what is OOP
object orientation program
type of programming based on the concept of object which contain data in the form of firlds and code in the form of procedures (methods)
what is procedural programming
programs build in the sequential order and call methods you want shared behaviors between pages in application
methods
behaviors that an object performs upon its internal data and even upon other code objects
what is class
blueprint that defines how to build an object
what does class contain
inctructions for creating new objects and has the ability to create those objects
syntax for class
def Class
#code about class
end
how do you add instance to class
instance_name = Class.new
what is instantiate
bringing a new object to life
different instances of a class are different object
true
what is ruby object notation
default way that ruby communicates to you that you are dealing with object or instance of particular class
instance dot notation
syntax for sending objects messages asking them to perform an operation of task
you should put all class in the same file
false put them each in their own file with the class name as the file name
where do you put a method if you want it to be available to all the instances in the class
in the class code block
def class
def method
method code
end
end
you can’t call the method in class on its own
correct
what are local varaibles
only acces in a specific local environment
you can access local variables in other methods
false
what are instance variable
variable that is accessible in any instance method in a particular instance of class
what does instance variable describe
attributes and properties of the instance such as name
what is setter methods
set attributes on the instances of our classes
what is getter methods
will return the value of the instance variable
what does macro return
more ruby code instead of a ruby data type
what is metaprogramming
writing og programs that operate other programs
what are the benefits of metaprogramming
automate repetive task
concise and descriptive
cons of metaprogramming
very hard to follow code that can obsures what is actually happening
what does attr_reader create
getter method
what does attr_writer create
setter method
when do you use a attr_accessor
when you need both a getter and setter function
macros are usually bad to use
false
initialize method is called automatically when .new is used
true
what is program’s design
manner in which you the programmer, organize and array the code
principals of object oriented design
Single responsibly principal
dont repeat yourself (DRY)
Line limits (methods 5 lines, classes 100)
what is domain modeling
representation of real-world concepts in software
what is self
special variable that points to the object that “owns’ the current executing order
what is self used for
decide which execution context to use at any point in the program
objects are aware of themselves and can use self
true
self keyword refers to what
the instance or object that the method is being called on
what is monkey patching
practice of adding methods to or altering ruby built in classes
its okay to use monkey patching in real applications
false
explicit receiver syntax
self.method where self is receiving the method
implicit receiver syntax
omits the self keyword when calling method (just the method is written)
you can’t use implicit receiver with setter functions
true
what are class methods
methods called on the class instead of instances
class variable scope
class scope- accessible to the entire class
what is an example of using a class method
getting the count of all the instances in a class
syntax for class method
class Class_name
@@element_count = 0
def self.count
@@album_count
end
end
difference in class constants and class variables
constants store data that doesn’t change and defines with all capitalize letters
syntax for accessing class constant outside of scope
Class : : CONSTANT
what are public methods
can call them from outside the scope of class declaration like on instance or the class itselfg
how do you call public methods
explicit receiver
who are the receiver of private methods
self
why use private methods
encapsulate functionality in a class
show that the method is depended on by other methods in your program
how do your create private method
add private above the methods that are private methods
steps to define custom error
- define the custom error class
- raise custom error
what is rescuing
rescues program when you get an error and allows program to continue to run
steps to handle custom error
- write custom message
- implement the rescue