More stuff Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

What is a regex?

A

It is a sequence of characters that form pattern matching rules, and is then applied to a string to look for matches.

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

What are the main use cases of regex?

A

The most common is string pattern matching.
Others:
1. Check if “ss” appears in the string “Mississippi”.
2. Print out the 3rd word of each sentence from a list of sentences.
3. Find and replace “Mrs” with “Ms” in a sentence.
4. Does a string start with “St”?
5. Does a string end with “art”?
6. Does a string have non-alphanumeric characters in it?
7. Are there any whitespace characters in the string?
8. Find and replace all non-alphanumeric characters in a string with “-“.

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

Use =~ to see if we have a match in our regex!

A
def has_a_well?(string)
  if string =~ /well/
    puts "We have a match!"
  else
    puts "No match here."
  end
end

has_a_b?(“Maxwell”)
=> “We have a match!”
has_a_b?(“welfare”)
=> “No match here.”

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

Use the “match” method to perform a regex comparison!

A
def has_a_well?(string)
  if /well/.match(string)
    puts "We have a match!"
  else
    puts "No match here."
  end
end

has_a_b?(“Maxwell”)
=> “We have a match!”
has_a_b?(“welfare”)
=> “No match here.”

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

Explain the difference between the following two code snippets!

a = "hi there"
b = a
a = "not here"      

AND

a = “hi there”
b = a
a &laquo_space;”, Bob”

A

In the first case, a’s value is assigned to b then a is reassigned, but b is still gonna b a’s formal value (it is linked there in the memory).
In the second case, the line of code
a &laquo_space;”, Bob”
did not result in reassigning a to a new string. Rather, it mutated the caller and modified the existing string, which is also pointed to by the variable b.
Both have “hi there, Bob” as their values.

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

What is the core idea of variables as pointers?

A

Some operations will mutate the actual address space in memory, thereby affecting all variables that point to that address space. Some operations will not mutate the address space in memory, and instead will re-point the variable to a new address space in memory.

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

What are procs?

A

Procs are blocks that are wrapped in a proc object and stored in a variable to be passed around.

e.g.

talk = Proc.new do |name|
puts “I am talking to #{name}”
end

talk.call “Bob”

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

How do we mark if the argument is a block in a method definition?

A

The ampersand (&) in the method definition tells us that the argument is a block.

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

What are the exception handling keywords?

A

begin
rescue
end

e.g.
names = [‘bob’, ‘joe’, ‘steve’, nil, ‘frank’]

names.each do |name|
  begin
    puts "#{name}'s name has #{name.length} letters in it."
  rescue
    puts "Something went wrong!"
  end
end
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is exception handling?

A

Exception handling is a structure used to handle the possibility of an error occurring in a program. It is a way of handling the error by changing the flow of control without exiting the program entirely.

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

What does the following error message tells us?

A

The stack trace first tells us where the error occurred and why: line 2 of greeting.rb, and the error was because the types don’t match. The error occured due to the call made in the ‘main’ context on line 6, which contains the line that called the method with incorrect arguments, line 2.

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

How to write large numbers to make them easier to read?

A

Use _ to separate them by 3 digits that increases readability.
e.g.
2_862_954 == 2862954

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

What is the end result of these two and why?

a = 7
def my_value(b)
b += 10
end

my_value(a)
puts a

AND

a = “Xyzzy”

def my_value(b)
b[2] = ‘-‘
end

my_value(a)
puts a

A

first: puts a =>7
second: puts a => Xy-zy

Numbers are immutable but strings are mutable that’s why puts a shows an updated (mutated) version of variable a.
(the element at index 3 is replaced by - )

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

What is the result of:

a = 7

def my_value(b)
    b = a + a
end

my_value(a)
puts a

A

Error, because methods have their own scopes and local variable a is not defined there.

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

Are local variables initialized inside the definition visible outside the method definition? And vica-versa?

A

No and no.

e.g.
a = 7

def my_value(b)
b = a + a 
end

my_value(a)
puts a

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

What is the result of the
following code and why?

a=7
array=[1,2,3]

array.each do |element|
a = element
end

puts a

A

3 because we deal with a method invocation that has a block, not a
method definition.

method invocation with blocks can modify local variables that are defined outside the block

17
Q

What is “heredoc”?

A

A heredoc is a way to define a multiline string, while maintaining the original indentation & formatting.

e.g.
page = <

18
Q

How can you get the current

time?

A

Using the Time class, Time#now method (Time.now)

19
Q

What does !! method do?

A

It turns any value into a boolean.

!!123 =>true

20
Q

What is the value of a variable initialized in a conditional statement?

A

Nil

if false
b = “hello”
end

b = nil

21
Q

When do you use git fetch and git pull?

A

To have the remote changes integrated to the local copy.

git pull could mess up the local working copy, while fetch wouldn’t.