Chapter 3 Take Advantage of Ruby's Smart Collections Flashcards

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

What is an easy way to intialize the following ruby code?
poem_words = [“twinkle”, “little”, “star”, “how”, “I”, “wonder”]

A

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}.

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

What are some different ways to write a hash literal?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How would you build a method that takes a varying number of arguments?

A

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.

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

What if you want a method to take an arbitrary set of arguments?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How many starred parameters can you have?

A

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…

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

What is * called?

A

Think of an exploding array, it’s called a splat.

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

How could you one like a loop conditional with a break?

A

loop {base_case; break if break_case}

ex:

loop{10; break if 11}

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

What is the logical equivilent of until?

A

while not

until

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

How could you use until for a one liner conditional loop?

A

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.

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

In ruby what is a Regular Expression?

A

A regular expression in ruby is treated as a built-in data type.

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

What syntax does ruby use to enclose a regex?

A

/Regular expressions are enclosed in forward slashes/

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

What are two ways to organize your regular expressions?

A

[sETs] and ranges [0-9A-Z]

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

What does the this operator do =~ ?

A

=~ scans for a match of a regex anywhere within a string.

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

How do you turn off special meanings of punctuation in regular expressions?

A

/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 ~.

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

In regular expressions what does a . do ?

A

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./

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

Are regular expressions case sensitive? How would you adjust the case sensitivity?

A

They are case sensitive by default. You can turn off case sensitivity in order to recognize both uppercase and lowercase letter by adding a little i to the end of your regular expression.

/PM/i will match both PM and pm

17
Q

What is a shorter way of matching any digit rather than a set of [123456789] or a range [1-9]?

A

\d “digit” will match any digit

/\d\d/ will match all the way up to 99.

18
Q

What’s the regex shortcut to match any “word character”?

A

\w will match any letter, number, or the underscore as well.

19
Q

What is the regex shortcut to match any “space”?

A

\s will match any whitespace character including vanilla space, tab or newline.

20
Q

How can you provide two options for a regular expression match?

A

This | that

AM | PM

Alternatives will match the regex before or after the pipe.

21
Q

What does the * do in regular expressions?

A

The asterisk * in a regular expression will match zero or more of the thing that came just before it.

For Example:

/R*uby/ will match:

RRRRRRRuby

Ruby

uby

22
Q

What combination of regular expression syntax will match anything?

A

.*

The period and astrisk will match anything because the period could be anything and the astrisk could match zero or any amount more of that anything before it.

23
Q

What is a powerful method to pair with a regular expression if you need to replace a match with something else?

A

gsub(/regex/, ‘*****’)

You can use gsub to replace any regular expression matches with your preffered replacement.

24
Q

How would you match a regular expression ‘A’t the begining of a string only?

A

\A at the begining of the regex

/\Aat the begining of the string match this regex only/

25
Q

How would you match a regex only with the end of a string?

A

\z

/match this only at the end of the string\z/

26
Q

What is a circumflex?

A

^ represents the circumflex in regular expressions it can be used to

/^match a regex with the begining of a string as well as the begining of each line/

27
Q

How do you match a regular expression with the end of a string and the end of each line?

A

$

The dollar sign at the end of a /regular expression$/ will match the end of a string and the end of each line.

28
Q

What is a :symbol in Ruby?

A

a :symbol is similar to a string, but it can not be changed, it is a muted string, and once it is set that is the only instance of that symbol.

A = :symbol

B = A

C = :symbol

A, B and C all refer to the same object

29
Q

What is public_methods?

A

The method public_methods when called on any object will return an array containing the names of all the public methods on that object.

Post Ruby 1.9 the names of the methods will be return as :symbols.

Prior to 1.9

30
Q

What is the difference between a binary operator and a unary operator?

A

A binary operator functions upon to operands while a unary operator functions upon a single operand. For example:

Binary: 2+3

Unary: -3

Binary and unary -(2+3) = -5

or even +(2-6) = 4

31
Q

How would you define a binary operator for example +?

A

def +(other)

end

32
Q

How would you define a unary operator? Use + and - as examples.

A

def +@

end

def -@

end

33
Q

What is operator overloading?

(also known as: ad hoc polymorphism , function overloading)

ad hoc (latin “for this”, in english usually means a solutions for a specific problem or task)

A

Operator overloading is where operators have different implementations based on their arguments.

It is a form of syntactic sugar. Ruby translates every expression involving programmer-definable operators into an equivalent expression where the operators are replaces with method calls.

34
Q
A
35
Q
A