Ruby_catch_all Flashcards

1
Q

What do programmers call the exclamation point (!)?

A

The bang operator

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

What does the (!) bang operator do?

A

It reverses the value.

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

What is a method?

A

A method is simply a definition of an action that can be performed on an object, i.e. like the .capitalize (method) that capitalizes text.

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

What are Arguments?

A

Arguments are pieces of information that are sent to a method to be modified or used to return a specific result.

We “pass” arguments to a method when we call it.

We put arguments into the parentheses following a method. For example, 1.+(2).

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

What are Parameters used for?

A

Parameters are used when you have data outside of a method’s scope, but you need access to it within the method’s scope.

def say(words)
puts words
end

You’ll notice that there’s a (words) after say in the method definition. This is what’s called a parameter.

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

Argument?

A

Additional information that is provided to a method when it is called. We put arguments into the parentheses following a method. For example, 1.+(2).

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

String concatenation?

A

string concatenation: Combining two strings together; accomplished with the concat() method or the + operator. Example: “zig” + “zag” returns “zigzag”.

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

Objects?

A

In Ruby, and most other programming languages, we call things objects .

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

Methods?

A

In Ruby, and most other programming languages, we call things objects and the actions you can tell those things to do methods.

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

Class?

A

I’ve been saying for a bit that there are different “types” of objects, but in Ruby, the real word is class. 2.3 is of the class Float; 2 is of the class Fixnum (for fixed number - a number without a decimal); and Strings like “hello!” are of the class String.

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

Instance?

A

Here’s another term to get familiar with: instance. Every object is an instance of a class. 2.3 is an instance of the Float class; 2 is an instance of Fixnum; and “hello!” is an instance of String. If there were a class called Person then you could have instances of it called mary, bob, and sam - they are all people because they belong to the class Person, but they are instances of the class because they are specific people. You can always see what class an object is by calling the class() method on it:

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

Variables

A

Variables store objects and you can think of them like a box with a special label that can hold one thing at a time, but you can change whatever is inside that box.

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

What is the receiver called when dealing with methods?

A

The object on which a method is called. Remember that the object receives the action of the method. Some methods permanently change the receiver and some do not. The String#upcase method does not change the receiver, but the String#upcase! method does.

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

return value:

A

return value: The object that is returned by the method after it has run; what we see in IRB after the method is executed.

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

bang

A

bang: The name of the exclamation point found at the end of Ruby methods that change the receiver when they are run.

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

What are parameters

A

Parameters are used when you have data outside of a method’s scope, but you need access to it within the method’s scope. If the method does not need access to any outside data, you do not need to define any parameters.

17
Q

Get the value of key b in the following hash.

h = {a:1, b:2, c:3, d:4}

A

h[:b]

or

h.fetch(:b)

18
Q

Add to this hash the key:value pair {e:5} in the following hash.

h = {a:1, b:2, c:3, d:4}

A

h[:e] = 5

19
Q

How do you move information out of an array, given the following array, output the email from the contact_data array.

contact_data =
[[“joe@email.com”, “123 Main st.”, “555-123-4567”],
[“sally@email.com”, “404 Not Found Dr.”, “123-234-3454”]]

A

contact_data[0][0]
contact_data[1][0]

this happens because the contact_data array is a nested array, there are 2 arrays here.. So contact_data[0] pulls the first array, and then the second [0] pulls the 1st value of first array like so => contact_data[0][0], contact_data[1][0]

this outputs :
=> “joe@email.com”
=> “sally@email.com”

20
Q

Given the following data structures. Write a program that moves the information from the array into the empty hash that applies to the correct person.

contact_data =
[[“joe@email.com”, “123 Main st.”, “555-123-4567”],
[“sally@email.com”, “404 Not Found Dr.”, “123-234-3454”]]

contacts = {“Joe Smith” => {}, “Sally Johnson” => {}}

A

contacts[“Joe Smith”][:email] = contact_data[0][0]
contacts[“Joe Smith”][:address] = contact_data[0][1]
contacts[“Joe Smith”][:phone] = contact_data[0][2]

contacts[“Sally Johnson”][:email] = contact_data[1][0]
contacts[“Sally Johnson”][:address] = contact_data[1][1]
contacts[“Sally Johnson”][:phone] = contact_data[1][2]

21
Q

What does shift method do?

array = [1, 2, 3, 4]

A

it removes the first item of an array and actually
permanently removes the item.

array = [1, 2, 3, 4]
array.shift

returns
=> 1

22
Q

Use Ruby’s Array method delete_if and String method start_with? to delete all of the words that begin with an “s” in the following array.

arr = [‘snow’, ‘winter’, ‘ice’, ‘slippery’, ‘salted roads’, ‘white trees’]

A

First use the delete_if method to iterate the array, then use the start_with? method to pull the keys out that start with ‘s’.

arr = [‘snow’, ‘winter’, ‘ice’, ‘slippery’, ‘salted roads’, ‘white trees’]

arr.delete_if do |key|
key.start_with?(‘s’)
end

23
Q

take the following array and turn it into a new array that consists of strings containing one word.

a = [‘white snow’, ‘winter wonderland’, ‘melting ice’,
‘slippery sidewalk’, ‘salted roads’, ‘white trees’]

(ex. [“white snow”, etc…] → [“white”, “snow”, etc…]. Look into using Array’s map and flatten methods, as well as String’s split method.

A

a = [‘white snow’, ‘winter wonderland’, ‘melting ice’,
‘slippery sidewalk’, ‘salted roads’, ‘white trees’]

words_split = a.map do |word|
word.split
end
p words_split.flatten

THis only work if you make a new variable = to a.map.

24
Q

What are objects?

A

In Ruby, and most other programming languages, we call things objects

25
Q

What is a way to create and new hash using the new hash method?

A

Hash.new

ex. dictionary = Hash.new()
returns
=> {}

26
Q

What is a conditional?

A

A conditional is a fork (or many forks) in the road.

27
Q

Add ages for Marilyn and Spot to the existing hash. ages = { “Herman” => 32, “Lily” => 30, “Grandpa” => 5843, “Eddie” => 10 }; additional_ages = { “Marilyn” => 22, “Spot” => 237 }

A

ages.merge!(additional_ages)