Chapter 4 Flashcards

1
Q

true %>

A

build in rails function (stylesheet_link_tag) to include application.css for all media types (including computer screens and printers.

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

helper functions

A

creation of new rails functions

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

define a method full_title if no page title is defined (using provide on that page), then print out “Rails Tutorial Sample App”, otherwise add a | and precede it by the page title

A

module ApplicationHelper

# Returns the full title on a per-page basis
def full_title(page_title = '')
base_title = "Ruby on Rails Tutorial Sample App"
if page_title.empty?
base_title
else
page_title + " | " + base_title
end
end
end
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

using full_title method, create a new title tag to use it:

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

steps to make home page with no custom page title

A

update home page to remove the provide function. update the test assert to test this

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

#

A

comments in ruby

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

string literal

A

zero or more characters grouped in quotes

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

concatenate two strings

A

“foo” + “bar”

“foobar”

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

interpolate a string and define meaning

A

first_name = “sam”
“#{first_name} Erd”
“Sam Erd”. interpolation is using a placeholderto insert a certain variable

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

printing a string

A

puts which appends a new line

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

nil

A

literally nothing

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

\n

A

new line

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

difference between ‘’ and “”

A

nothing, there identical, except ruby wont interpolate into single-quoted strings

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

is everything in ruby an object?

A

yes. even nil is an object.

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

what is an object?

A

object is a model of something with behavior and functions and attributes. place to store data about an entity.

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

empty?

A

method returns true or false

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

if s is nil, return “nil”, else if s is empty, return “empty”, else if, s includes “foo”, return “foo”

A
if s.nil?
"nil"
elsif s.empty?
"empty"
elsif s.include?("foo")
"foo"
else
"lol"
end
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

puts “empty” if x and y are empty, puts “one is empty” is x or y is empty, puts “x” if x is not empty
puts “xy” if neither x and y are empty

A

puts “empty” if x.empty? && y.empty?
puts “one is empty” if x.empty? || y.empty?
puts “x” if !x.empty?
puts “xy” if !x.empty? && y.empty?

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

puts two variables if neither is empty

A

puts “#{x} #{y}” if !x.empty? && !y.empty?

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

.to_s

A

convert virtually any object to a string.

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

nil.to_s

A

””

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

nil.empty?

A

error

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

nil.to_s.empty?

A

true

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

”“.nil?

A

false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
nil.nil?
true
26
puts a string using interpolation unless its empty
puts "#{foo}" unless foo.empty?
27
!!nil
false
28
!!0
true
29
``` def string_message(str = '') returns whether string is empty or not. what is output for: string_message("foo") string_message("") string_message ```
string is nonempty string is empty string is empty its possible to leave out the argument entirely because the code contains a default argument, which in ths case is the empty string
30
return string if its empty
return "empty" if str.empty?
31
last return in a function is unnecessary why?
returned regardless of return keyword
32
module ApplicationHelpter
gives us a way to package together related methods which can be then mixed in to Ruby classes using "include"
33
"foo bar yo" split this string into an array
"foo bar yo".split | ["foo", "bar", "yo"]
34
split "fooxbarx" by the x's
"fooxbarx".split("x") | ["foo", "bar"
35
get first, second, last elenment of an element
a. first a. second a. last
36
a.last == a[-1]
true
37
x = a.length
length of a string or could be array
38
``` a = [41, 8, 17] empty include 41 sort it reverse the order shuffle it ```
a. empty? a. include?(41) a. sort (can be used for strings too!) a. reverse a. shuffle
39
a.sort!
actually changes to array a unlike a.sort which just outputs a sorted array
40
push elements into array
x.push(6) | x
41
a = [42, 8, "foo"] a.join join using comma
"428foo" a.join(', ') "42, 8, foo"
42
0..9.to_a
undefined method for 9:fixnum
43
(0..9).to_a
gives array from 0 to 9, 10 elements
44
make a string array out of the words foo bar baz | and then show range of it
a = %w[foo bar baz] | a[0..2]
45
a = (0..9).to_a | return array from index 1 up to last index
a[1..-1]
46
range 1 through 5 and put our each number times 2
(1..5).each { |i| puts 2 * i} or (1..5).each do |i| puts 2 * i end
47
(1..5).each { |i| puts i}
each method passes range to block. |i| block variable. executes for each value in the range
48
puts "betelgeuse" three times
3.times {puts "betelgeuse" } | takes no variables
49
(1..5).map { |i| i ** 2 }
[1, 4, 9, 16, 25] produces new array
50
map out [a b c] into an array and change characters to uppercase or lower case
%w[a b c].map { |char| char.upcase(or char.downcase) }
51
map A B C into array and lower case it using symbol
%w[A B C].map(&:downcase)
52
``` test "should get home" do get :home assert_response :success assert_select "title", "Ruby on Rails Tutorial Sample App" end ```
do keyword that the body of the test is a block. test method takes in a string argument and a block, and then executes the body of the block as part of running the test suite.
53
user = {}
empty hash
54
user["first_name"] = "michael"
key "first_name", value "michael"
55
user["first_name"]
returns value of key "first_name
56
create hash user with first name vampie in one line
name = { "first_name" => "vampie" }
57
in rails whats more common to use than strings as hask keys
symbols
58
symbol
looks like string but prefixed with colon. :name symbols dont contain value or objects, like variables. used as consistent name in a code. compared to String objects, its more efficient to use symbols. ``` current_situation = "good" creates string object and stores in memory current_situation = :good are only singe reference values only initialized once. ```
59
create hash with symbols
user = { :name => "f", :email => "at" }
60
accessing undefined keys return what?
nil
61
create hash with special hash symbol
user = { name: "f", email: "at" }
62
create nested hashes user with name and email
params[:user] = { name: "f", email: "at" } | nested hashes are heavily used by rails
63
create each do for hash
hash.each do |key, value| puts "key: #{key.inspect} has value #{value.inspect}" end
64
inspect methdo
returns a string with a literal representation of the object
65
puts (1..5).to_a
``` 1 2 3 4 5 ```
66
puts (1..5).to_a.inspect
[1, 2, 3, 4, 5]
67
puts :name, :name.inspect
name | :name
68
puts "it\n", "it\n".inspect
it | "it\n"
69
stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true why no parenthesis?
in ruby, they are optional. there would be a parenthesis around it when hashes are the last arguments to a function, the parenthesis are optional
70
stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true explain this now
calls the stylesheet_link_tag function with two arguments: a string, indicating the path to the stylesheet, and a hash with two elements, indicating the media type and telling Rails to use the turbolinks feature added in Rails 4.0. data-track contains hyphens which are not allowed for symbols so we have to use the old syntax for it
71
s = String.new("foobar")
constructor to create string
72
a = Array.new([1, 2, 3])
array constructor
73
h = Hash.new(0)
(hash constructor) sets all nonexistent keys to return 0 instead of nil
74
s.class.superclass
string -> object -> basicObject -> nil
75
create class Word with method palindrom? that returns if a string in reverse is equal to the string forward
``` class Word def palindrome?(string) string == string.reverse end end ```
76
create palindrome method with class that inherits from string object
class Word
77
how can we add palindrome? to string class
class String create palindrome? method end now every string object has palindrome? method
78
blank? method
specific to rails since its a rails extension
79
controller = StaticPagesController.new
create controller object
80
attr_accessor :name, :email
attr_accessor allows us to get and set @name and @email instance variables
81
``` def initialize(attributes = {}) @name = attributes[:name] @email = attributes[:email] end ```
empty hash. will return nil if there is no :name key and for :email.
82
``` def formatted_email "#{@name} " end ```
returns name
83
require './example_user'
include ruby code into your session or code from a file in the current directory
84
we will see starting in chapter 7 that initializing objects using hash arguments, a technique known as mass assignment, is common in rails
true