Chapter 3 Take Advantage of Ruby's Smart Collections Flashcards
What is an easy way to intialize the following ruby code?
poem_words = [“twinkle”, “little”, “star”, “how”, “I”, “wonder”]
poem_words = %w{ twinkle little star how I wonder }
This will initialize the same ruby array of strings.
This is the literal form for an array of strings. %w{ some strings inside}.
What are some different ways to write a hash literal?
freq = { “I” => 1, “don’t” => 1, “like” => 1, “spam” => 963 }
____________________________________________
book_info = { :first_name => ‘Jack’, :last_name => ‘Corley’}
book_info_v2 = { first_name: ‘Jack’, last_name: ‘Corley’ }
both of above get called the same..
book_info[:first_name] == book_info_v2[:first_name]
How would you build a method that takes a varying number of arguments?
some awesome ruby code here
If you have a basic set of arguments and want your callers to be able to omit some of the arguments you can specify defaults to those arguments.
Here is an example of a method that could take either one or two arguments when it is called:
def load_font( name, size = 12 )
end
You must call the method with the argument name but not size unless you want the size to be different then 12.
What if you want a method to take an arbitrary set of arguments?
If you stick an asterisk before one of your parameter names that parameter will soak up any extra arguments passed to the method.
The value of the starred parameter will be any array containing all the extra arguments.
def echo_all( *args )
args.each { |arg| puts arg }
end
The above method will take any number of arguments and print them out.
How many starred parameters can you have?
You can only have one starred parameter but it can be pretty much anywhere in your parameter list.
This means you can sometimes rely on Ruby to manufacture an array for you…
What is * called?
Think of an exploding array, it’s called a splat.
How could you one like a loop conditional with a break?
loop {base_case; break if break_case}
ex:
loop{10; break if 11}
What is the logical equivilent of until?
while not
until
How could you use until for a one liner conditional loop?
do this until that
no closing end required but the until has to be in the middle between the base case and the break case condition.
In ruby what is a Regular Expression?
A regular expression in ruby is treated as a built-in data type.
What syntax does ruby use to enclose a regex?
/Regular expressions are enclosed in forward slashes/
What are two ways to organize your regular expressions?
[sETs] and ranges [0-9A-Z]
What does the this operator do =~ ?
=~ scans for a match of a regex anywhere within a string.
How do you turn off special meanings of punctuation in regular expressions?
/In a regular expression backslashes will omit puncations special meaning./
If you did not use the \ before the period above the period would match any string character including r % and ~.
In regular expressions what does a . do ?
A period . will match any string character including %, r and ~.
/To omit this functionality add a \ backslash before the . to get a regular period character./