Chapter 4 Flashcards

You may prefer our related Brainscape-certified 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
Q

nil.nil?

A

true

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

puts a string using interpolation unless its empty

A

puts “#{foo}” unless foo.empty?

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

!!nil

A

false

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

!!0

A

true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q
def string_message(str = '')
returns whether string is empty or not. what is output for:
string_message("foo")
string_message("")
string_message
A

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

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

return string if its empty

A

return “empty” if str.empty?

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

last return in a function is unnecessary why?

A

returned regardless of return keyword

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

module ApplicationHelpter

A

gives us a way to package together related methods which can be then mixed in to Ruby classes using “include”

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

“foo bar yo” split this string into an array

A

“foo bar yo”.split

[“foo”, “bar”, “yo”]

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

split “fooxbarx” by the x’s

A

“fooxbarx”.split(“x”)

[“foo”, “bar”

35
Q

get first, second, last elenment of an element

A

a. first
a. second
a. last

36
Q

a.last == a[-1]

A

true

37
Q

x = a.length

A

length of a string or could be array

38
Q
a = [41, 8, 17]
empty
include 41
sort it
reverse the order
shuffle it
A

a. empty?
a. include?(41)
a. sort (can be used for strings too!)
a. reverse
a. shuffle

39
Q

a.sort!

A

actually changes to array a unlike a.sort which just outputs a sorted array

40
Q

push elements into array

A

x.push(6)

x

41
Q

a = [42, 8, “foo”]
a.join
join using comma

A

“428foo”
a.join(‘, ‘)
“42, 8, foo”

42
Q

0..9.to_a

A

undefined method for 9:fixnum

43
Q

(0..9).to_a

A

gives array from 0 to 9, 10 elements

44
Q

make a string array out of the words foo bar baz

and then show range of it

A

a = %w[foo bar baz]

a[0..2]

45
Q

a = (0..9).to_a

return array from index 1 up to last index

A

a[1..-1]

46
Q

range 1 through 5 and put our each number times 2

A

(1..5).each { |i| puts 2 * i}

or

(1..5).each do |i|
puts 2 * i
end

47
Q

(1..5).each { |i| puts i}

A

each method passes range to block. |i| block variable. executes for each value in the range

48
Q

puts “betelgeuse” three times

A

3.times {puts “betelgeuse” }

takes no variables

49
Q

(1..5).map { |i| i ** 2 }

A

[1, 4, 9, 16, 25] produces new array

50
Q

map out [a b c] into an array and change characters to uppercase or lower case

A

%w[a b c].map { |char| char.upcase(or char.downcase) }

51
Q

map A B C into array and lower case it using symbol

A

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

52
Q
test "should get home" do
  get :home
  assert_response :success
  assert_select "title", "Ruby on Rails Tutorial Sample App"
end
A

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
Q

user = {}

A

empty hash

54
Q

user[“first_name”] = “michael”

A

key “first_name”, value “michael”

55
Q

user[“first_name”]

A

returns value of key “first_name

56
Q

create hash user with first name vampie in one line

A

name = { “first_name” => “vampie” }

57
Q

in rails whats more common to use than strings as hask keys

A

symbols

58
Q

symbol

A

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
Q

create hash with symbols

A

user = { :name => “f”, :email => “at” }

60
Q

accessing undefined keys return what?

A

nil

61
Q

create hash with special hash symbol

A

user = { name: “f”, email: “at” }

62
Q

create nested hashes user with name and email

A

params[:user] = { name: “f”, email: “at” }

nested hashes are heavily used by rails

63
Q

create each do for hash

A

hash.each do |key, value|
puts “key: #{key.inspect} has value #{value.inspect}”
end

64
Q

inspect methdo

A

returns a string with a literal representation of the object

65
Q

puts (1..5).to_a

A
1
2
3
4
5
66
Q

puts (1..5).to_a.inspect

A

[1, 2, 3, 4, 5]

67
Q

puts :name, :name.inspect

A

name

:name

68
Q

puts “it\n”, “it\n”.inspect

A

it

“it\n”

69
Q

stylesheet_link_tag ‘application’, media: ‘all’,
‘data-turbolinks-track’ => true

why no parenthesis?

A

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
Q

stylesheet_link_tag ‘application’, media: ‘all’,
‘data-turbolinks-track’ => true
explain this now

A

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
Q

s = String.new(“foobar”)

A

constructor to create string

72
Q

a = Array.new([1, 2, 3])

A

array constructor

73
Q

h = Hash.new(0)

A

(hash constructor) sets all nonexistent keys to return 0 instead of nil

74
Q

s.class.superclass

A

string -> object -> basicObject -> nil

75
Q

create class Word with method palindrom? that returns if a string in reverse is equal to the string forward

A
class Word
def palindrome?(string)
string == string.reverse
end
end
76
Q

create palindrome method with class that inherits from string object

A

class Word

77
Q

how can we add palindrome? to string class

A

class String
create palindrome? method
end
now every string object has palindrome? method

78
Q

blank? method

A

specific to rails since its a rails extension

79
Q

controller = StaticPagesController.new

A

create controller object

80
Q

attr_accessor :name, :email

A

attr_accessor allows us to get and set @name and @email instance variables

81
Q
def initialize(attributes = {})
    @name  = attributes[:name]
    @email = attributes[:email]
  end
A

empty hash. will return nil if there is no :name key and for :email.

82
Q
def formatted_email
    "#{@name} "
  end
A

returns name

83
Q

require ‘./example_user’

A

include ruby code into your session or code from a file in the current directory

84
Q

we will see starting in chapter 7 that initializing objects using hash arguments, a technique known as mass assignment, is common in rails

A

true