w1d1 Flashcards

1
Q

Explain the following code:

arry[1..6]

A

Returns the 2nd through 7th elements of the ‘arry’ array

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

What is the difference between [2..4] and [2…4]

A

Three periods is not inclusive of the last element; two periods is.

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

Explain the following code:

arry[2..-1]

A

Returns the third through the last element of the array.

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

T/F: Never modify an array while iterating through it.

A

T; You can get stuck in an infinite loop

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

What is an advantage of Array#concat?

A

It modifies the first array by including references to the second array; very little memory cost.

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

Explain Array#include? and Array#index

A

include? returns a Boolean for finding the argument, #index returns the index of the element as an integer or nil if it is not found.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
Explain the array methods:
#sort, #sort!, #shuffle, #sample
A

sort sorts an array, with the smallest element at index 0

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

T/F: It doesn’t matter if arrays are heterogeneous (contain more than one type)

A

F; They can, but should be homogenous whenever possible.

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

Y/N: Will the following code execute?

“this is my sentence”[5, 2]

A

Yes; when given arguments like [int, int], the first argument is the starting index of the substring and the second argument is the length of the substring. For this reason, the code returns “is”

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

Explain the following string methods: #strip, #chomp, #gsub

A

strip removes leading and trailing whitespace

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

When a hash is used with an enumerable, what gets passed to the block?

A

|key, value|

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

When merging hashes with #merge, what will happens when a key is shared but each hash has a different value for the key?

A

The hash which is being merged (the argument) into the other hash takes precedence.

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

What is a quick way to check if a hash has a key?

A

has_key?

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

What is the difference between Enumerable#each and Enumerable#map (also known as Enumerable#collect)? When should you use them?

A

each returns the original UNMODIFIED array, while #map returns a new array with the computed values.

Use #each when you only want the method’s side-effects; use #map when you want to use the returned (computed) values.

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

Explain Enumerable#inject

A

We pass inject an initial value and a block. The code block takes the current accumulator value and the next element in the Enumerable . The block is run, and the accumulator value is updated with the block’s result. When all elements have been processed, the accumulator value is returned.

nums = [1, 2, 3, 4, 5]
nums.inject(0) do |accum, element| # accum is initially set to 0, the method argument
accum + element
end

This code returns 15 (the sum of all the elements)

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

Explain Enumerable#select and Enumerable#count

A

select returns an array consisting of elements from the enumerable for which the given code block returns true.

17
Q

What is the difference between Enumerable#include?, Enumerable#any?, Enumerable#all?, and Enumerable#none?

A

include? returns true if the argument is equal to any item in the collection.

The other 3 methods work similarly. #any? returns true if at least one element of the collection returns true given the code block, while #all? checks if all elements return true and #none? checks if all elements return false.

18
Q

What is the difference between Kernel#puts and Kernel#p ?

A

puts calls #to_s, whereas #p calls #inspect

19
Q

T/F: The following code returns true:

nil.nil?

A

T; this is the only case where this will happen.

20
Q

What methods can be used to check the type of an object?

A

class and #is_a?(datatype)

21
Q

What happens if you do not overload #== for a class you define?

A

It will always return false, unless you are comparing an object to itself.

22
Q

What is “technical debt”

A

Code can become unwieldy when there is a rush for a deadline, so you can take out technical debt by working with “smelly” code and refactoring at another time, although remember that this is a delicate balance; debt can catch up with you!

23
Q

How can we use the byebug debugger?

A

Add ‘require ‘byebug’’ to the top of your file, then insert the ‘debugger’ keyword where you want your breakpoint(s).

24
Q

How can we add a breakpoint within byebug?

A

$break linenum

25
Q

What is the byebug command to view the stack trace?

A

$wtf

26
Q

What is the difference between ‘next’ and ‘step’ in byebug?

A

‘next’ executes the line and advances to the next line, while ‘step’ steps into the code and follows its execution. Type ‘finish’ to get out of the step.

27
Q

What do ‘display’ and ‘where’ do in byebug?

A

‘display’ watches the value of the variable (given as an argument), and ‘where’ displays the call stack.

28
Q

Explain the following error messages:

NameError

Uninitialized Constant

NoMethodError

ArgumentError

TypeError

LoadError

SyntaxError

A

A NameError exception is thrown when you try to use a variable or method that hasn’t been defined.

“Uninitialized constant” means that Ruby is trying to find a class (or other constant), but didn’t find it.

NoMethodError is thrown when a method or variable that does not exist/ can’t be located is called

ArgumentError is for the wrong number of arguments

A TypeError may be thrown if you pass the wrong type of thing to a method.

A LoadError occurs when a file is attempted to be loaded into a program but cannot be found.

Writing ungrammatical Ruby code will net you a SyntaxError . This tells you that Ruby didn’t understand your code.

29
Q

The typical source file contains how roughly how many lines of code and how many classes?

A

1 class and less than 300 lines.

30
Q

When using the ‘require’ keyword with our own files, what must be added to the beginning of the filename?

A

’./’

31
Q

What is the difference between a library file and a script file?

A

Some Ruby source files contain definitions of classes and methods meant to be loaded by other Ruby code (called library files), while others are intended to be executed as a program (called script files).

32
Q

What is the guard to add conditional execution to a file?

A

if $PROGRAM_NAME == __FILE__
scriptactions
end