Ruby basics + advanced Flashcards

1
Q

What is ruby?

A

It is a programming language

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What does RVM stand for?

A

Ruby version manager

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What does RVM let you do?

A

It lets you switch between different versions of ruby.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are variables?

A

They are the building blocks of a program and they let you store information.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How much information can a variable store?

A

Variables can store a lot of data, full algoritms, blocks and lambdas

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do you set a variable in ruby?

A

you give it a name and set it equal to the information

ex variable_1 = ‘A string’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How can you print out objects to the terminal?

A
  • puts
  • p
  • print
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is puts?

A

puts will return nil and print out the object in a new line. Puts also iterates over the values.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is p?

A

p will return the value and print out the object

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do you get user input form the terminal?

A

you use the gets method

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is chomp?

A

chomp deletes the extra character that your gets method adds to the input.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What kinds of variables are there?

A
  • Local variables
  • Global variables
  • Instance variables
  • Constants
  • Class variables
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is a local variable?

A

A local variable is a variable that limits itself to a local scope of where it is declared.
It is only available to that specific method or package

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is a global variable?

A

A global variable written with a $ before the variable name is available all over the application. And it’s a terrible idea. because the last time you set the variable will declare it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is a instance variable?

A

An instance variable is written with a @ sign before the variable name.
The instance variable is available for that instance in other places.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is a constant?

A

A constant is a variable is written with CAPITAL letters and is a variable that is not supposed to be changed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What is a class variable?

A

A class variable is written with @@ is a variable that is available to that instance of the class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What is a string?

A

A string is a object where you store a collection of characters. These must be contained in single or double quotes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

What is string interpolation?

A

String interpolation is a method where you can display dynamic values inside a string. Everything between a #{ } will be embedded ruby. You must use double quotes on the string when working with string interpolation.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

What is string manipulation?

A

String manipulation is when you use methods to change the strings.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

what is a bang? ( ! )

A

When you see the ! in the end of a method it means that it is changing the original version of the variable .

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

What is string substitution?

A

String substitution is a method in ruby. You use .gsub!( ‘original’ , ‘the change’)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

What is the strip method?

A

it removes the extra spaces in a string in the beginning and in the end of a string

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

What is the split method?

A

The split method converts each word in a string to an array.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

What is the order of execution in ruby?

A

Please Excuse My Dear Aunt Sally =

Paranthesis, Exponents, Multiplication, Division, Addition, Subtraction

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

What is the difference between integers and floats?

A

A float has decimals and integer doesn’t.

make sure to use decimal to get the proper output.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

What is the difference between floats and decimals?

A

Floats do not have many decimal points, but decimals can have a lot of numbers to be accurate.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

How do you declare a method?

A

def snake_case
some logic
end

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

How do you call/run a method?

A

you just type the method name

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
30
Q

What is the difference between puts and return

A

puts is used for debugging purposes and does not store a value.
return prints and stores the value and is used in production applications.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
31
Q

What is the difference between a class method and a instance method?

A

You can call the class method directly on the class But you need to create an instance to be able to call an instance method.

ex
SomeClass.some_class_method

ex
variable = Invoice.new
variable.some_instance_method

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
32
Q

How do you write a class method?

A

you add self before the method name in a class method.

def self.method_name
something
end

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
33
Q

How do you call a class with a class method

A

SomeClass.some_class_method

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
34
Q

What are Procs?

A

Procs are blocks of code (methods) that can be stored inside variables.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
35
Q

How do you write a Proc?

A

ex
variable = Proc.new { |argument1, argument2 | some code }

or
ex
variable = Proc.new do | argument1, argument 2 |
some code
end
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
36
Q

How do you call a Proc?

A

proc_variable[ argument1, argument2]

or
proc_variable.call(argument1, argument2)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
37
Q

How do write a lambda?

A

variable = lambda { |argument1, argument2 | some code }

or

variable = -> (argument1, argument2) { |argument1, argument2 | some code }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
38
Q

How do you call a lambda?

A

lambda_variable[ argument1, argument2]

or

lambda_variable.call(argument1, argument2)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
39
Q

What is the difference between a Proc and Lambda?

A

1 lambdas count the number of arguments given (you must give right number of arguments).

2 Procs do not count the arguments (Procs ignore excess arguments).

3 Procs and lambdas have return differences. Procs do not return everything but lambdas do

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
40
Q

What are method arguments?

A

The method arguments are the raw material provided by the user.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
41
Q

What are named arguments?

A

It is when you need to name your arguments when calling them, good for readability, to avoid confusion and you can give arguments in random order.

ex 
def write_name(first_name: , last_name: )
  puts first_name
  puts last_name
end

write_name(first_name: “Faraz”, last_name: “Naeem”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
42
Q

What are default arguments?

A

Default arguments are arguments that are already written in the method, so that you only need to write out the argument when you want to overwrite the argument in the method.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
43
Q

What is a splat argument?

A

A Splat argument allows you to pass in multiple arguments into a method but it treats the arguments like an array.
ex

def method(*players)
some_code
end

method(argument1, argument2, argument3, argument4 …)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
44
Q

What is a keyword based splat argument?

A

a keyword based splat argument allows you to pass in an hash, then the

def method(**players)
some_code
end

method(some_hash)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
45
Q

What are optional arguments?

A

Optional arguments allows you to use arguments that you specifically want, and it will ignore other.

ex
def method_name(options ={})
puts [:some_key]
end

method_name(some_key: “hej”, another_key: “då”)

=> hej

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
46
Q

What is a while loop?

A

It is a bit of code that will run as long as the conditions are true, it is very important to increment the value otherwise the loop will run forever.

while i < 10
puts “hey”
i += 1
end

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
47
Q

What is the each loop?

A

the each loop is an iterator.

ex
variable.each do |value|
some_code for value
end

ex
variable.each { |value| some code for value}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
48
Q

What is the for loop?

A

the for loop executes code once for each element in expression.

ex
for i in 0..4
puts i
end

ex
for variable [, variable …] in expression [do]
code
end

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
49
Q

What is an nested iterator?

A

It is when you are iterating on a collection inside another collection. (two hashes inside one hash)

ex

hash.each do | main_hash_value, sub_hash |
  puts main_hash_value
  sub_hash.each do |key, value|
  some code
end
end
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
50
Q

What is the select method?

A

The select method selects a value from a collection and runs your code.

ex
(1..10).to_a.select do |value|
value.even?
end

ex
(1..10).to_a.select { |value| value.even? }

ex
(1..10).to_a.select(&:even?)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
51
Q

What is the map method?

A

The map method takes an enumerable object and a block and runs the block for each object.

ex
array.map { |value| value.some_method }

ex
array.map(&:some_method)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
52
Q

What is the inject method?

A

The inject method is used to combine number (addition subtraction, multiplication and so on)

ex
array.inject(&amp;:+)
ex
array.inject(&amp;:-)
ex
array.inject(&amp;:*)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
53
Q

How do you create an array?

A

ex
x = [1, 2, 34 ]

ex
y = Array.new
y[0] = 543

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
54
Q

How do you remove items from an array?

A

you can use the delete method

ex (by the values)
array.delete(values)

ex (by index)
array.delete_at(index)

ex ( by if)
array.delete_if { |value| some_code }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
55
Q

What is the join method?

A

The join method is used to join the different values in an array

ex
array = [1, 2, 3]
array.join(‘+’)
=> “1+2+3”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
56
Q

What is the push method?

A

The push method is when you add an element to an array at the end of the array

ex
array = [1, 2, 3]
array.push(4)
=> [1, 2, 3, 4]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
57
Q

What is the pop method?

A

The pop method is used to delete the last element in an array.

ex
array = [1, 2, 3, 4]
array.pop
=> [1, 2, 3]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
58
Q

How do you add an element to an array=

A

You add them
by push or by index

ex
array[index] = value

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
59
Q

What is a hash?

A

A hash is an key value collection (also called dictionary)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
60
Q

How do create an hash?

A

ex (modern syntax)
hash = {first: 1, second: 2}

ex (old syntax)
hash = {“first” => 1, “second” => 2}

ex (old, but with symbol)
hash = {:first => 1, :second => 2}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
61
Q

How do you return a value from an hash based on a key?

A

hash[:key]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
62
Q

How do you delete an element from an hash?

A

with the delete method
ex
hash.delete(:key)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
63
Q

How do you iterate over just the keys in a hash?

A

You use the each_key method

ex
hash.each_key do |key|
some_code
end

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
64
Q

How do you iterate over just the values in a hash?

A

You use the each_value method

ex
hash.each_value do |value|
some_code
end

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
65
Q

How do you add to a hash?

A

hash[:key] = value

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
66
Q

How do you reverse the keys and the values in a hash?

A

you use the invert method

ex
hash = {:first => 1, :second => 2}
hash.invert
=> {1 => :first, 2 => :second}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
67
Q

How do you merge two hashes?

A

with the merge method

ex
hash.merge(another_hash)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
68
Q

How do you convert a hash to an array

A

hash.to_a

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
69
Q

How do write an if statement?

A
ex
if condition
  some_code
  elsif condition
   some_code
  else
   some_code
end

ex (simple)
some_code if conditional

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
70
Q

What is an unless statement?

A

An unless statement runs code unless some condition becomes true

ex
unless conditional
some_code
end

ex
some_code unless conditional

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
71
Q

How do you chain multiple conditionals together(compund conditional)?

A

With the && or || methods

ex (and symbol both must be true)
if conditional && conditional
some_code
end

ex (or symbol one must be true)
if conditional || conditional
some_code
end

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
72
Q

How do you set up a class in ruby?

A
ex
class ClassName

end

everything within the class will belong to the class

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
73
Q

What is an initializer method?

A

It is a method that will run some code every time you create a new instance of the class. The initialize method will need some arguments that you need to pass in when you are creating a new instance of the class.

ex
def initialize(argument1, argument2)
some_code
end

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
74
Q

Why is inheritance important?

A

In order not to repeat code it is easier to give inheritance to other classes. than rewrite the code for every class.

You have a parent class and the other classes (children) inherit from it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
75
Q

How many responsibilities should a class have?

A

only one

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
76
Q

What is a public method?

A

It means that the method is visible to others.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
77
Q

What is private method?

A

It means that only that specific class has access to that specific methods.

A secret method should only be called from within the class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
78
Q

What is polymorphism?

A

It is when you override a method from a parent class with a method from a child class. You use polymorphism to give custom behaviour.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
79
Q

What is the super keyword?

A

It is when you want the same method both in the parent and child class to be called, instead of the child class method overriding the parent class method (polymorphism)

You write the super keyword in the child class whitin the method when you want it to be called.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
80
Q

How do you create a file?

A

ex
File.open(“file-path”, ‘options’) { |x| x.write(“something to write”)}

ex (alternate way)
variable = File.new(“file-path”, ‘options’)
variable.puts(“Some content”)
variable.close

options :
r - reading
a - appending to a file
w - just writing
w+ reading and writing
a+ open a file for reading and appending
r+ - opening a file for updating both reading and writing
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
81
Q

How do you read from a file?

A

ex
variable = File.read(“file path”)

you can now treat the content of the file as you want.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
82
Q

How do you delete a file?

A

ex

File.delete(“file path”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
83
Q

How do you update a file in ruby?

A

ex ( will add stuff to the file)

File.open(“file path” “a”) {|f| some_code}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
84
Q

What is the basic syntax for error handling in ruby?

not using Rspec

A
ex
begin
  some code
rescue
  some code
end
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
85
Q

How should you write errors in ruby? (not using Rspec)

A
ex
begin 
  some code
rescue StandarError => variable
  puts "error message #{variable}"
end
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
86
Q

What is a regular expression?

A

A regular expression is a special sequence of characters that helps you match or find other strings or sets of strings using a specialized syntax held in a pattern.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
87
Q

what is the basic syntax for regular expressions?

A

=~ /matcher/

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
88
Q

What is grep?

A

Grep is a method that is used to search through collections and objects.

ex
array.grep(search_object)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
89
Q

What are ruby gems?

A

Ruby gems are basically ruby code packaged.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
90
Q

What is metaprogramming in ruby?

A

It is the process of writing code that writes itself during runtime.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
91
Q

What is the w%( ) method?

A

it allows you to create an array from a string without adding commas and such.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
92
Q

What is the freeze method?

A

The freeze method makes an object unable to change.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
93
Q

How do you interact with an API with ruby?

A

You need a gem (httparty) to interact and you also need a class with methods.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
94
Q

What is a URI?

A

Uniform Resource Identifiers is a string of characters used to identify a resource.

The most common form of URI is the Uniform Resource Locator (URL),

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
95
Q

Do you need to use a web framework like Rails or Sinatra to work with APIs?

A

No

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
96
Q

What is a popular gem for working with APIs?

A

httparty

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
97
Q

What type of data is typically sent back as an API response?

A

JSON

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
98
Q

What is the sort method?

A

The sort method allows you to sort the elements inside an array.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
99
Q

What is the super keyword?

A
super calls the method of the parent class, if it exists. 
it passes all the arguments to the parent class method as well.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
100
Q

What are modules?

A

Modules are a way of grouping together methods, classes, and constants.
Modules give you two major benefits.
- Modules provide a namespace and prevent name clashes.
- Modules implement the mixin facility.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
101
Q

What is a mixin

A
Mixins give you a wonderfully controlled way of adding 
functionality to classes. However, 
their true power comes out when the code in 
the mixin starts to interact with code in the class that uses it.

A mixin can basically be thought of as a set of code
that can be added to one or more classes to
add additional capabilities without using inheritance.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
102
Q

What is the & prefix?

A

the & prefix allows us to pass in a code block as a parameter like { print ‘Ho!’ }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
103
Q

What is Ruby?

A

Ruby is a high-level programming language

  • Ruby is Interpreted (no need for a compiler)
  • Object-oriented (allows users to manipulate data structures)
  • Easy to use
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
104
Q

What kind of data types are there in Ruby?

A
Integer = Number
Numeric = class of number like float, fixnum
Float = decimal number
NilClass = The class of the singleton object nil.
Hash = A Hash is a dictionary-like collection of unique keys and their values.
Symbol = Symbol objects represent names and some strings inside the Ruby interpreter.
Array = A list of objects
Range = A Range represents an interval—a set of values with a beginning and an end.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
105
Q

What is a variable?

A

One of the most basic concepts in computer programming is the variable. You can think of a variable as a word or name that grasps a single value.

my_num = 25

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
106
Q

What arithmetic operator are there in ruby?

A

addition +
subtraction -

multiplication *
division /
modulo %
exponentiation **

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
107
Q

What is the difference between puts and print?

A

print just prints whatever you give it to the screen
puts (“put string”) prints out what you give it with an empty line

So the difference is that puts has one more line
added.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
108
Q

What is a method in ruby?

A

They are built in abilities that objects have.

For example methods can show you the length of a string or reverse the string.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
109
Q

What is an interpreter?

A

An interpreter is a program that takes your code and runs it, it shows the result in your console

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
110
Q

How do you use a method?

A

Methods are used with a .

my_string = ‘i love pizza’ –> my_string.length –> 12

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
111
Q

How do write a comment in ruby?

A

you use #

Everything behind the (on the same line) # will be commented out

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
112
Q

How do you do a multiline comment in Ruby?

A

You use =begin and =end

Everything in between will be commented out, you can use this to explain complicated concepts.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
113
Q

What is a naming convention?

A

A naming convention is how the community writes code, local variables should be written in lowercase letters and words.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
114
Q

How do you declare or set a variable?

A

You use the =

my_number = 12

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
115
Q

How can you call multiple methods on a variable?

A

by chaining

name.method1.method2.method3

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
116
Q

What is chaining?

A

Chaining is that you call multiple methods on a variable

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
117
Q

What is control flow?

A

Control flow is that we can select different outcomes depending on the environment and the input.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
118
Q

What is an if statement?

A

Ruby takes an if statement and decides if its true and runs code accordingly.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
119
Q

What is an else statement?

A

The else statement is the continuation of the if statement. This code runs after the if statement is false.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
120
Q

What is an elsif statement?

A

An elsif statement gives the if else statement more options.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
121
Q

What is an unless statement?

A

An if statement checks if something is true, An unless statement checks if something is false and runs code accordingly.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
122
Q

What is a comparator?

A

A comparator checks if two values are equal or not

we use == to check if they are equal and != if we want to check if two values are not equal.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
123
Q

What are the comparator for less or equal than?

A

less than <

less than or equal to <=

greater than >

Greater than or equal to >=

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
124
Q

What are boolean operators?

A

Boolean operators are either true or false
- && (and)

  • | | (or)
  • ! (not)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
125
Q

What is an and operator?

A
  • && (and), The expression is true if both statements before and after && are true

true && true => true
true && false => false
false && true => false
false && false => false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
126
Q

What is an Or-operator?

A
  • | | (or) is an operator when at least one of the expressions are true
    true || true # => true

true || false # => true
false || true # => true
false || false # => false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
127
Q

What is an Not-operator?

A

A not operator (!) makes any statement after the expression false
!true= false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
128
Q

What is gets.chomp?

A

Gets chomp is for getting user input from the terminal

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
129
Q

What is the .downcase method?

A

the .downcase method takes the user input and converts it do lower case letters.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
130
Q

What is the .include? method

A

The include? method checks if the user input contains what we want

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
131
Q

What is the .gsub! method?

A

The .gsub method stands for global substitution and changes all the chosen inputs.

string_to_change.gsub!(/s/, “th”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
132
Q

What is string interpolation ?

A

String interpolation adds a value to a string.

print “Adios, #{my_string}!”

133
Q

What is a While loop?

A

The while loop runs code as long as the conditions are true.

counter = 1
while counter < 11
puts counter
counter = counter + 1
end
134
Q

What is an infinite loop?

A

An infinite loop is a loop that runs forever.

They need to be avoided at all cost.

135
Q

What is an untill loop?

A

An until loop runs as long as the conditions are not met.

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

What is an assignment operator?

A

An assignment operator is used to update a variable.

+=, -=, *=, and /=.

137
Q

What is an for loop?

A

The for loop runs a code between a range.

for current_iteration_number in 1..100 do
puts “Hello world, this is number #{current_iteration_number}”
end

138
Q

What is an inclusive range?

A

An inclusive range is a range that includes the last number in the range
1..3 (two dots)

for num in 1..20 (don’t forget the in)
puts num
end

139
Q

What is an exclusive range?

A

An exclusive range is a range where the last number is not included in the range.

1..3 (three dots)

for num in 1…20 (don’t forget the in)
puts num
end

140
Q

What is an iterator?

A

An iterator is just a Ruby method that repeatedly invokes a block of code.

141
Q

What is the next keyword?

A

The next keyword is used to skip a step.

142
Q

What is an array?

A

An array is a variable with multiple items in it
my_array = [1, 2, 3, 4]

An array is also indexed starting from 0.

143
Q

How do you get user input?

A

gets.chomp

144
Q

What is The .each Iterator ?

A

.each method, which can apply an expression to each element of an object, one at a time.

object.each do | item |
# Do something
end
145
Q

What is the .times Iterator?

A

The .times method is like a super compact for loop: it can perform a task on each item in an object a specified number of times

10.times { print “Chunky bacon!” }

146
Q

What is the .split Method?

A

it takes in a string and returns an array. If we pass it a bit of text in parentheses, .split will divide the string wherever it sees that bit of text, called a delimiter. For example,

text.split(“,”)

147
Q

What is an array index?

A

Each element in the array has what’s called an index. The first element is at index 0, the next is at index 1, the following is at index 2, and so on.

148
Q

How do you access an object in an array with the index?

A

With brackets

array = [5, 7, 9, 2, 0]
array[2]
# returns “9”, since “9”
# is at index 2

149
Q

What are multidimensional arrays?

A

It’s an array of arrays

150
Q

What are hashes?

A

A hash is a collection of key-value pairs

151
Q

How do you create a hash?

A

You use the Hash.new method.
by setting a variable to Hash.new

my_hash = Hash.new

152
Q

How do you add objects to a hash?

A

We add object to hash with bracket notation:

pets = Hash.new
pets["Stevie"] = "cat"
153
Q

How do you accesses objects in a hash?

A

We accesses it by using the same method like an array:
puts pets[“Stevie”]

pets = {
"Stevie" => "cat",
"Bowser" => "hamster",
"Kevin Sorbo" => "fish"
}

puts pets[“Stevie”]

154
Q

What do we call it when we loop over a hash or an array?

A

We call it that we iterate over the hash or array.

155
Q

How do we iterate over arrays?

A

We use the .each method
numbers = [1, 2, 3, 4, 5]

numbers.each { |element| puts element }

156
Q

How do you iterate over multidimensional arrays?

A
s.each do | sub_array |
  sub_array.each do | y |
    puts y
  end
end
157
Q

How do you iterate over a hash?

A

As the same as in an Array, but you need to have two arguments instead of one.

restaurant_menu.each do | item, price |
puts “#{item}: #{price}”
end

158
Q

What is a ‘Histogram’?

A

A visual representation of data is called a histogram.

159
Q

What is a method?

A

A method is a reusable section of code written to perform a specific task in a program.

160
Q

Why do we use methods?

A
  1. Easy to fix bugs

2. We use separation of concerns

161
Q

How do you define a method?

A

You use the def syntax along with end

162
Q

What is the structure of a method?

A
def method_name
the_method_body

end

163
Q

What is calling a method/function?

A

We call a method or a function when we want to use it.

164
Q

What is a NameError?

A

You get a NameError when your program can’t find the method.

165
Q

What is an argument?

A

The argument is the piece of code you actually put between the method’s parentheses when you call it

166
Q

What is a parameter?

A

parameter is the name you put between the method’s parentheses when you define it.

167
Q

What are splat arguments?

A

Splat arguments are arguments preceded by a *, which tells the program that the method can receive one or more arguments.

168
Q

What is return?

A

When we want to get back a a value from the method instead of printing it to the console.

169
Q

What are blocks?

A

Blocks are nameless methods.

Blocks can be defined with either the keywords do and end or with curly braces ( { } ).

They are only called once

170
Q

Why do we use code blocks?

A

Because we want to use abstraction, which is basically making the code simpler.

171
Q

What is a combined comparison operator?

A

The combined comparison operator is used to compare two ruby objects

172
Q

what are hashes?

A

hashes are collections of key-value pairs

173
Q

What happens when you try to access a key that doesn’t exist?

A

You will get the value nil

174
Q

What does nil mean?

A

nil is Ruby’s way of saying “nothing at all.”

175
Q

How do you set a new default value in a hash?

A

my_hash = Hash.new(“Trady Blix”)

Now if you try to access a nonexistent key in my_hash, you’ll get “Trady Blix” as a result.

176
Q

What are symbols?

A

Symbols are not strings and can only be used once. They are used for referencing method names and as hash keys.

177
Q

How do you write a symbol?

A

You write it:

:symbol_name

178
Q

Why are symbols good as hash keys?

A
  1. They cant be changed once they are created (immutable)
  2. Only one symbol with the same name can exist so they save memory
  3. Symbols are faster.
179
Q

How do you convert between strings and symbols?

A

with
.to_s (symbol to string)

.to_sym (string to symbol)
.intern (string to symbol)

180
Q

How do you assign symbols as keys to a value?

A

You use the new syntax instead of the old hash syntax

new_hash = {
one: 1,
two: 2,
three: 3
}
181
Q

How can we filter values from a hash?

A

We use the .select method

182
Q

How do we iterate over just a value?

A

With the .each_value method

183
Q

How to we iterate over just a key?

A

With the .each_key method

184
Q

How do you write a single line if statement?

A

puts “It’s true!” if true

185
Q

How do you write a single line unless statement?

A

puts ‘Hello’ unless false

186
Q

What is a ternary conditional expression?

A

A if/else statement is a ternary expression beacuse it takes three arguments:
- A boolean

  • a expression to evaluate if the boolean is true
  • a expression to evaluate if the boolean is false

boolean ? Do this if true: Do this if false

187
Q

What is a case statement?

A

A case statement is a if/else statement that is used when there are a lot of conditions to check.

case language
when "JS" then put "Websites!"
when "Python" then puts "Science!"
when "Ruby" then puts "Web apps!"
else puts "I don't know!"
end
188
Q

What is a conditional assignment?

A

A conditional assignment assigns a variable to a name if the name is nil

favorite_animal = ‘cat’
favorite_animal | |= ‘dog’
print favorite_animal => cat

favorite_animal = nil
favorite_animal | |= ‘dog’
print favorite_animal => ‘dog’

189
Q

What is a implicit return?

A

It means that ruby will return a value even though you did not require it.

190
Q

What is a short-circuit evaluation?

A

It means in a conditional statement with two conditions the second condition is evaluated only when the first condition is not enough to determine the value of expression

false && true

191
Q

What is the concatenation operator?

A

It is «
and used to push in a element to the end of an array or a string.

[1, 2, 3] &laquo_space;4 => [1, 2, 3, 4]
‘Faraz ‘ &laquo_space;‘Naeem’ => ‘Faraz Naeem’

192
Q

What is string interpolation?

A

{ x } its when you add a value to a string, no need to convert it.

193
Q

What is refactoring?

A

Refactoring means that one should make the code simpler and more readable whitout changing its properties or function

194
Q

What is a ruby block?

A

A block is a bit of code that can be executed

You can use do …end or { } brackets

195
Q

What is the collect method?

A

The collect method takes a block and applies the expression in the block to every element in an array

my_nums = [1, 2, 3]
my_nums.collect { |num| num ** 2 }
# ==> [1, 4, 9]
196
Q

What is the yield keyword?

A

The yield keyword, when used inside the body of a method will allow you to call that method with a block of code and pass the torch or yield to that block.

Think of the yield keyword as stop executing the code in this method, go and instead execute the code in this block. Then return to the code in the method.

197
Q

Are blocks objects?

A

No blocks are not objects and can therefore not be saved to a variable.

198
Q

What are Procs?

A

Procs are saved blocks that you can name and turn into a method.

199
Q

What are Procs used for?

A

Procs are used for keeping the code dry.

200
Q

What is Dry

A

A programming concept : Don’t Repeat Yourself.

201
Q

How do you create a Proc?

A

with Proc.new

cube = Proc.new { |x| x ** 3 }

202
Q

What are the advantages of Procs?

A

1) They are objects

2) They can be reused.

203
Q

How can you call on a Proc?

A

By using the .call method

204
Q

How do you convert a symbol into a Proc?

A

with the & symbol.

205
Q

How do you write a lambda?

A

lambda { | param | block }

206
Q

What is the difference between lambdas and procs?

A

Lambda checks the number of arguments.
Lambdas count the arguments.

A lambda passes control back to the calling method.
Procs do not count the argument passed to them it just ignores needless arguments.

207
Q

When are Curly braces required in ruby?

A

When you want to put the code in one single line.

208
Q

Why would you like to use a Proc instead of a regular Method?

A
  • Gives more flexibility
  • you can store more in a variable
  • required in the database syntax
209
Q

What are attributes?

A

Attributes are specific properties of an object.

"Matz".length
# ==> 4
Matz is an object
.lenght is a method
4 is an attribute
210
Q

What is a class?

A

A class is a way to organize and produce objects with similar attributes and methods.

Basically a blueprint.

211
Q

How do you write a class?

A
class ClassName
# some code

end

You use CamelCase for the class name

212
Q

What is the initialize method?

A

The initialize method is used to boost up the class, it gives us attributes that we can start up with.
It will run every time we create the class.

213
Q

What is an instance variable?

A

An instance variable makes the variable available in other parts of the application

214
Q

What is scope of a variable?

A

The scope of a variable is where the variable is accessible for the rest of the program. Some variables are not accessible to other parts of the program.

215
Q

What are global variables?

A

Variables that are available everywhere

216
Q

What are local variables?

A

Variables that are only available inside certain methods.

217
Q

How do you write instance variables?

A

you write them with a @

@variable_name

218
Q

How do you write class variables?

A

You write them with two @@

@@variable_nameHow do yo wr

219
Q

How do you write global variables?

A

1) You just define the variable outside the method or class.

2) If you are writing the global variable from inside a method or a class just write $ before the variable.

220
Q

How should you create variables?

A

You should only create them with a limited scope so that they can only be changed from a few places.

221
Q

What is inheritance?

A

inheritance is the process by which one class takes on the attributes and methods of another.

222
Q

What is the inheritance syntax?

A

you use the < symbol to show the inheritance.

class DerivedClass < BaseClass
# Some stuff!
end

This means that DerivedClass inherits from BaseClass.

223
Q

What is override?

A

If you want a class to not to take the attributes of the baseclass

224
Q

What is the super keyword?

A

The super keyword is used when you want to accesses the attributes or the methods of a superclass

225
Q

How do you set up a class in ruby?

A
class SomeClassName
end
226
Q

What is inheritance?

A

Inheritance means that a class has access to all methods and behaviors of another class. You use the < to show the inheritance.

227
Q

What can you store in a class in ruby?

A

You can store both methods and data in a class.

228
Q

What is the getter method in a class?

A

The getter method allows you to retrieve data or values from a class.

229
Q

What is the setter method in a class?

A

The setter method allows you to set values in a class.

230
Q

What is the attr_accessor?

A

The attr_accsessor stands for attribute accessor and is used instead of the getter and setter methods.

231
Q

What does instantiation mean?

A

When you create a class it’s like creating a blueprint, to instantiate a class means to build the house from a blueprint. To actually use the class to create.

232
Q

What is an initializer method?

A

An initializer method is a method that will run every time you create a new instance of a class.

233
Q

What are optional values?

A

When you are not sure that you want to pass on a value as an argument, you can assign the value to be optional. It will only be assigned if you give an argument.

You write value = something if you want it to be optional

234
Q

Why do we use inheritance in ruby?

A

We use inheritance because repeating code is a bad practice.

235
Q

We use inheritance because repeating code is a bad practice.

A

It is when you use third-party services instead of creating your own.

236
Q

What is a public method?

A
A public method is a method inside a class that can be used by anyone working with that perticular class.
These are really important if you are giving access to these methods via an API.
237
Q

What are private methods?

A

Private methods are methods that can only be called from whitin the class and not by an external service or user.

238
Q

How do you make a method private?

A

You add the keyword private to a section of your class, and all methods that are added below that keyword will be private.

Ex
class Example

def method_1
some_code
end

private

def method_2
some_code
end

end

239
Q

What is polymorphism in ruby?

A

Polymorphism is when the behavior from the parent class is overridden by the child class.

240
Q

How do you use Polymorphism?

A

You use it by defining a new class with the same method name, the method of the child class will be run instead of the method from the parent class.

241
Q

What is the super keyword and when do you use it?

A

The super keyword allows us to use both methods from parent and child classes instead of polymorphism.

You use it by adding the keyword inside the method you want the parent class to override

242
Q

What is the single responsibility principle?

A

It means that each class and module in the application should only focus on a single task.

243
Q

Why are private methods used?

A

To make sure external users can’t access or harm your software.

244
Q

What do private methods do?

A

They make sure that the method can’t be called from outside the class.

245
Q

What is a public method?

A

A public method is a method that can be called from outside the class. All methods are public by default in ruby.

246
Q

What is the attr_reader used for?

A

It is used for accessing a variable in a method/class

247
Q

what is the attr_writer used for?

A

It’s used for changing the variable in the method/class

248
Q

What is the attr_accessor used for?

A

It is used as bot a attr_reader and attr_writer.

249
Q

What is an module in Ruby?

A

A module is like a class, but it can’t create instances or have subclasses.

Modules are good when using constants, because constants don’t change, but variables do.

250
Q

How do you write a module?

A
module ModuleName
# Bits 'n pieces

end

251
Q

How do you write constants in ruby?

A

You write them in ALL_CAPS

like PI

252
Q

What is namespacing?

A

The main purpose of a module is to separate methods and constants into named spaces, and this is called namespacing.

253
Q

What is the scope resolution operator?

A

It is used for telling ruby where to look for something.

Math : : PI means that Ruby should look for the PI inside the Math module.

254
Q

What is require?

A

When the interpreter doesn’t have a module you need to bring it in with require. It basically runs another file.

require ‘module’

255
Q

What is include?

A

The include method takes all the methods from one module and includes them in the current method.

256
Q

What is mixin?

A

Mixin is when you blend additional behaviour from a module into a class. Meaning that we can customise a class whit out writing new code.

257
Q

What is the extend keyword?

A

The extend keyword means that the class can use the methods as opposed to the instance of the class.

258
Q

How to delete a element from the end of an array?

A

arr = [1, 2, 3]
arr.pop

=> arr = [1, 2]
POP!!!

259
Q

How to delete an element from the beginning of the array

A

arr = [1, 2, 3]
arr.shift

=> arr = [ 2, 3 ]
(Will return the value that was deleted in the terminal)

260
Q

How to Delete an element at a given position

A

arr = [1, 2, 3]
arr.delete_at(1)

=> arr = [ 1, 3 ]

261
Q

How to Delete all occurrences of a given element

A

arr = [1, 2, 3, 5, 6, 8, 5]

arr.delete(5)

=> [1, 2, 3, 6, 8]

262
Q

How to select elements that satisfy a given criteria?

A

arr = [ 3, 4, 2, 1, 2, 3, 4, 5, 6]
arr.select {|a| a > 2}

[3, 4, 3, 4, 5, 6]

263
Q

What is non destructive selection?

A

It is when the arrays remain unchanged after the selection.

264
Q

How do you permanently delete objects in an array?

A

you use the delete_if method

arr.delete_if { | a | a < value }

265
Q

How do you keep some of the objects in an array?

A

You use the .keep_if method

arr.keep_if {|a| a < value }

266
Q

What is a splat operator?

A

A Splat operator gives us the method to insert multiple arguments, without predefining them.

267
Q

What is a keyword based splat argument?

A

It is a mix between a keyword argument and a splat arguments.

268
Q

What are optional arguments?

A

Optional arguments allows you to pass in any kind of arguments and then utilize them. They can lead to misleading bugs.

269
Q

With what methods can you print to the ruby console?

A

Using puts, print, and p

270
Q

What is the puts method?

A

The puts method will return a value to you and display it by printing each object in a new line. The puts method iterates through the objects and displays individual values.

271
Q

What is the p method?

A

The p method will print the object in its code form.

272
Q

What is the print method?

A

The print method will return a value to you and display it by printing each object. The print method iterates through the objects and displays individual values.

273
Q

How do you get user input from the user?

A

With gets and chomp methods.

274
Q

What is the gets method?

A

the gets method will prompt the user to give a input, it will however add a /n to the string or integer.

275
Q

What is the .chomp method?

A

The .chomp method gets rid of the character: \n at the end of the line

276
Q

What kind of variables are there?

A
  • Local variables
  • Global variables
  • instance variables
  • Constants
  • Class variables
277
Q

What is a local variable?

A

A local variable is a variable that is declared inside a method or a loop. The scope is that it is limited to only that method or loop

Undefined local variable or method

278
Q

What is a global variable?

A

A global variables is a variable that is available for the entire application everywhere you write it with a $ before the variable name. Not a good idea to use them because they are hard to keep track of.

279
Q

What is an instance variable?

A

An instance variable (@variable_name) is only available in during a particular occurance and it is not available in other methods.

Why not make it a local variable? Beacuse we want to call the method in other files, for that to happen we need to use an instance variable .
Ex view and controller.

280
Q

What is an constant?

A

Generally constants are values that do not change.
You use all capital letters when declaring constants like PI = 3,14

Ruby does allow you to change a constant but it is poor programming practice to change constants.

281
Q

What is a class variable?

A

Class variables ($$variable_name) are variables that are only used in a specific class. They are rarely used in practical situations.

282
Q

What is a string?

A

A string is a data type which contains a set of characters, often a word or sentence. They need to be wrapped inside quotation marks

283
Q

What is string interpolation?

A

It is the ability to integrate dynamic values inside a string. They are enclosed with a #{ } you can even run code you want to display inside the brackets. But it is not recommended to type entire methods with string interpolation.

284
Q

What is string manipulation?

A

String manipulation is to alter the format or the value of a string.
Methods like upcase, downcase is used.

285
Q

What is method chaining?

A

It is when we join multiple methods and use them.

286
Q

What is string substititon

A

It is when you change a value in the string. We use the gsub method. which stands for global substitution

287
Q

Why does one add a bang?

A

Adding a bang permenantly changes the the object with the changes.

288
Q

What is the strip method?

A

The strip method takes away the spaces in the beginning and the end of a string.

289
Q

What is the split method?

A

The split method takes a string and turns it into an array

290
Q

What is a While loop?

A

While something is true the code will run, there is a danger of infinite loops in these cases, make sure to add a increment.
Ex i= i+1

291
Q

What is the each loop?

A

The each loop is an iterator

that goes through an object and executes the code on each single object.

292
Q

What is the one line each iterator?

A

arr.each { | x | do_something x }

293
Q

What is the syntax for the each iterator?

A

arr.each do |x|
do_something x

end

294
Q

What are nested iterators?

A

nested iterators are used when you want to extract and operate on hashes and arrays with multiple objects.

295
Q

What is the select method in ruby?

A

The select method is a method that automatically iterates through a collection and retrieves the value you want.

296
Q

How do you use the select method in ruby?

A

There are three ways to use the select method in ruby:
- something.select do |x|

some_code
end”
- something.select { |x| x.some_code}
- something.select(&:some_code)

297
Q

How can you create arrays?

A

1) variable = [1, 2, 3]
2) variable = Array.new

then add elements by id number y[0] = 543

3) nums = Array.
4) nums = Array[1, 2, 3, 4,5]
5) array = %w{ a b c d }

298
Q

How do you delete values from arrays?

A

With the delete method

array.delete(value)

299
Q

How do you delete an element from an array using the index?

A

With the delete at method

array.delete_at(index_number)

300
Q

How do you delete elements from array that fit into an criteria?

A

with the delete if method

array.delete_if { |x| some_code }

301
Q

What is the join method?

A

The join method returns a string created by converting each element of the array to a string, separated by the given separator. If the separator is nil, it uses current $,. If both the separator and $, are nil, it uses empty string.

[ “a”, “b”, “c” ].join #=> “abc”

[ “a”, “b”, “c” ].join(“-“) #=> “a-b-c”

302
Q

What is the pop method?

A

It allows you to delete the last element of an array

1) array.pop

303
Q

What is the push method?

A

it allows you to add an element to end of an array
1) array.push(element)

2 array.push(element1, element2, element3)

304
Q

What is an hash?

A

The hash is an key:value collection that can store values.

305
Q

How do you create a new hash?

A

1) hash = { first_key: ‘first element’ , second_key: ‘second_element’ } (this is the modern way to make a hash)
2) hash = { ‘first_key’ => ‘first_element’ }
3) hash = { :fist_key => ‘first element’ }

306
Q

How do you grab an element from an hash?

A

hash{:chosen_key]

307
Q

How do you delete elements from a hash?

A

you use the delete method

hash.delete(:key)

308
Q

How do you iterate over just a hash key?

A

With the each_key method

hash.each_key do |key|
some_code
end

309
Q

How do you iterate over just a hash value?

A

With the each_value method

hash.each_value do |value|
some_code
end

310
Q

How do you add to a hash?

A

hash[:key] = value

311
Q

How do you invert the key and the values in a hash?

A

You use the .invert method

hash.invert

312
Q

How do you merge two hashes?

A

With the .merge method

hash1.merge(hash_2)

313
Q

How do we convert a hash into an array?

A

With the to_a method

hash_1.to_a

314
Q

How do you view all keys in a hash?

A

With the .key method

hash_1.key

315
Q

How do you view all values in a hash?

A

with the .value metod

hash_1.value

316
Q

What are conditional statements?

A

Conditional statements are structures that run code if conditions are met.

317
Q

What is an if statement?

A

An if statement is a structure that runs if the given statement is true, otherwise it runs the else statement.

if conditional [then]
run some_code
elsif conditional [then]
run some_code
else
run some_code
end
318
Q

What is the unless conditional?

A

The unless conditional is a method that runs if the conditional is not true

319
Q

What are compounded conditionals?

A

It is when you add multiple conditionals with

| | or &&

320
Q

What is Enumerable?

A

Enumerable is a module that contains methods to traverse, search and sorting collections. It si added by default in the Array and Hash classes. But can be added to a class by including it.

321
Q

What is the select method?

A

The select method picks out what you want and puts it into a new array.

big_orders = orders.select { | o | o >= 300 }

322
Q

What is the reject method?

A

The reject method finds objects and puts it into a new array just like the select method. The difference is that it rejects all objects that does match the criteria.

323
Q

What is the any? method?

A

The any? method searches for a object inside a collection that matches a criteria, The execution stops once the object is found and return true.

324
Q

What is the detect method?

A

The detect method searches for a object inside a collection that matches a criteria, The execution stops once the object is found and returns the first object it found.

325
Q

What is the reduce method?

A

The reduce method is used to reduce a list down to a single value by combining objects.

[1, 2, 3].reduce(0) { |sum, n| sum + n } # => 6

0 is the initial value

326
Q

What is the map method?

A

The method is used to map (transform) the elements in one array into another new array

[1, 2, 3].map { |n| n * 2 } # => [2, 4, 6]

327
Q

What is the partion method?

A

The partition method is used to separate a collection into multiple parts giving you an array of arrays.
collection = [1, 2, 3, 4, 5, 6]
even, uneven = collection.partition {|v| v.even? }
even => [2, 4, 6]
uneven => [1, 3, 5]

328
Q

How do you create a block method?

A

by defining a method that includes yield, and using the method to call a block.

329
Q

How can you create your own iterator method?

A

By opening up the class and adding the method to it. You have to use self if you want to add the object to the front instead of passing it as a parameter