More stuff Flashcards
What is a regex?
It is a sequence of characters that form pattern matching rules, and is then applied to a string to look for matches.
What are the main use cases of regex?
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 “-“.
Use =~ to see if we have a match in our regex!
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.”
Use the “match” method to perform a regex comparison!
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.”
Explain the difference between the following two code snippets!
a = "hi there" b = a a = "not here"
AND
a = “hi there”
b = a
a «_space;”, Bob”
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 «_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.
What is the core idea of variables as pointers?
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.
What are procs?
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 do we mark if the argument is a block in a method definition?
The ampersand (&) in the method definition tells us that the argument is a block.
What are the exception handling keywords?
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
What is exception handling?
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.
What does the following error message tells us?
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 to write large numbers to make them easier to read?
Use _ to separate them by 3 digits that increases readability.
e.g.
2_862_954 == 2862954
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
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 - )
What is the result of:
a = 7
def my_value(b) b = a + a end
my_value(a)
puts a
Error, because methods have their own scopes and local variable a is not defined there.
Are local variables initialized inside the definition visible outside the method definition? And vica-versa?
No and no.
e.g.
a = 7
def my_value(b) b = a + a end
my_value(a)
puts a