Chapter 4 Flashcards
What is a helper?
A user-defined function in Rails
Where are files for helpers stored?
app/helpers
T/F: Each helper file corresponds to an ERB layout, and provides its functions to it.
T
What is the command to start the rails console?
$rails console
How are ‘puts’ and ‘gets’ pronounced? Why?
as “put ess” and “get ess”, short for put string and get string.
T/F: The ‘print’ function appends a newline character.
F; only ‘puts’ does this
T/F: Single-quote strings support interpolation and escape characters.
F; only double quoted strings allow for interpolation and escape characters; single-quoted strings print all characters as their literal values.
How can we guarantee we never have to deal with the error produced by the following code:
nil.empty?
always change it to a string:
nil.to_s.empty?
returns true
What are the ONLY two ruby objects which return false when used as a boolean?
‘false’ and ‘nil’
How are ‘if’ and ‘unless’ statements related?
Both can be placed after a statement to determine if the preceding statement is executed by evaluating the proceeding statement
What is the point of the ‘!!’ operator?
Used as a unary operator it converts any object to a boolean value
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?
When used in the same manner as ‘unless’, if statements do not include an ‘end’ keyword
What is the point of the ‘module’ keyword?
It allows classes to use the modules with the ‘include’ keyword
T/F: Ruby arrays can contain more than one type.
T
How can a range be converted to an array?
(0..9).to_a
What does the following code do:
a = %w[foo bar baz quux]
Creates an array of 3 strings
T/F: Ranges work with letters and numbers.
T
What is the equivalent of the following code:
%w[A B C].map(&:downcase)
%w[A B C].map{|c| c.downcase}
What does the following code return:
(‘a’..’z’).to_a.shuffle[0..7].join
7 random letters
What is the following code shorthand for:
h1 = { name: “Michael Hartl”, email: “michael@example.com” }
h1 = { :name => “Michael Hartl”, :email => “michael@example.com” }
What needs to be passed to a block when a hash uses a method requiring a block?
|key, value|
What does the ‘inspect’ method do?
Returns the literal value of an object
> > puts “It worked!”, “It worked!”.inspect
It worked!
“It worked!”
> > puts :name, :name.inspect
name
:name
What is the ‘p’ function shorthand for?
puts object.inspect
Can symbols include ‘-‘ characters?
No
T/F: Curly braces are always required when defining hashes.
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.
Why is ‘h = Hash.new’ different from ‘h = Hash.new(0)’
The latter means that any query to a nonexistent key will return the default value given as the constructor argument.
What is the difference between an instance method and a class method?
Class methods are called on the class itself, instance methods are called on individual instances.
Can the ‘superclass’ method be chained?
Yes, in fact it HAS to be chained:
> > s.class.superclass.superclass
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?
Either extend the String class or make your new class inherit from the String class
If no inheritance is defined when defining a new class, what will the class inherit from?
The ‘Object’ class, which inherits from ‘BasicObject’.
This effectively means that ALL Ruby objects inherit from ‘BasicObject’
What is the ‘self’ keyword for?
To specify the object itself.
What does the rails method ‘blank?’ do, and how is it different from ‘empty?’
‘blank?’ returns true if the string is empty OR consists of only whitespace; ‘empty’ returns false if consisting of only whitespace
In Ruby require statements, why is the ‘./’ necessary for the current directory?
’.’ is Unix for the current directory