Chapter 4 Flashcards

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

What is a helper?

A

A user-defined function in Rails

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

Where are files for helpers stored?

A

app/helpers

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

T/F: Each helper file corresponds to an ERB layout, and provides its functions to it.

A

T

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

What is the command to start the rails console?

A

$rails console

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

How are ‘puts’ and ‘gets’ pronounced? Why?

A

as “put ess” and “get ess”, short for put string and get string.

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

T/F: The ‘print’ function appends a newline character.

A

F; only ‘puts’ does this

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

T/F: Single-quote strings support interpolation and escape characters.

A

F; only double quoted strings allow for interpolation and escape characters; single-quoted strings print all characters as their literal values.

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

How can we guarantee we never have to deal with the error produced by the following code:

nil.empty?

A

always change it to a string:

nil.to_s.empty?

returns true

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

What are the ONLY two ruby objects which return false when used as a boolean?

A

‘false’ and ‘nil’

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

How are ‘if’ and ‘unless’ statements related?

A

Both can be placed after a statement to determine if the preceding statement is executed by evaluating the proceeding statement

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

What is the point of the ‘!!’ operator?

A

Used as a unary operator it converts any object to a boolean value

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

How does the ruby interpreter know whether to read an if statement as the beginning of an if-else block or a condition at the end of a statement?

A

When used in the same manner as ‘unless’, if statements do not include an ‘end’ keyword

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

What is the point of the ‘module’ keyword?

A

It allows classes to use the modules with the ‘include’ keyword

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

T/F: Ruby arrays can contain more than one type.

A

T

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

How can a range be converted to an array?

A

(0..9).to_a

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

What does the following code do:

a = %w[foo bar baz quux]

A

Creates an array of 3 strings

17
Q

T/F: Ranges work with letters and numbers.

A

T

18
Q

What is the equivalent of the following code:

%w[A B C].map(&:downcase)

A

%w[A B C].map{|c| c.downcase}

19
Q

What does the following code return:

(‘a’..’z’).to_a.shuffle[0..7].join

A

7 random letters

20
Q

What is the following code shorthand for:

h1 = { name: “Michael Hartl”, email: “michael@example.com” }

A

h1 = { :name => “Michael Hartl”, :email => “michael@example.com” }

21
Q

What needs to be passed to a block when a hash uses a method requiring a block?

A

|key, value|

22
Q

What does the ‘inspect’ method do?

A

Returns the literal value of an object

> > puts “It worked!”, “It worked!”.inspect
It worked!
“It worked!”

> > puts :name, :name.inspect
name
:name

23
Q

What is the ‘p’ function shorthand for?

A

puts object.inspect

24
Q

Can symbols include ‘-‘ characters?

A

No

25
Q

T/F: Curly braces are always required when defining hashes.

A

F; they can be omitted if the hash is the LAST argument in a function call, because the interpreter will know to add all excess arguments to the hash.

26
Q

Why is ‘h = Hash.new’ different from ‘h = Hash.new(0)’

A

The latter means that any query to a nonexistent key will return the default value given as the constructor argument.

27
Q

What is the difference between an instance method and a class method?

A

Class methods are called on the class itself, instance methods are called on individual instances.

28
Q

Can the ‘superclass’ method be chained?

A

Yes, in fact it HAS to be chained:

> > s.class.superclass.superclass

29
Q

Say we have a class that takes a string as an argument to all of its methods: what is a smart alternative to defining an entirely new class?

A

Either extend the String class or make your new class inherit from the String class

30
Q

If no inheritance is defined when defining a new class, what will the class inherit from?

A

The ‘Object’ class, which inherits from ‘BasicObject’.

This effectively means that ALL Ruby objects inherit from ‘BasicObject’

31
Q

What is the ‘self’ keyword for?

A

To specify the object itself.

32
Q

What does the rails method ‘blank?’ do, and how is it different from ‘empty?’

A

‘blank?’ returns true if the string is empty OR consists of only whitespace; ‘empty’ returns false if consisting of only whitespace

33
Q

In Ruby require statements, why is the ‘./’ necessary for the current directory?

A

’.’ is Unix for the current directory