Chapter 4 Flashcards

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

true %>

A

includes application.css for all media types(computer screens and printers ect). to rails developer, this line looks simple.

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

define full_title helper method. returns base title if no page title is defined

A
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
3
Q

you created full_title helper method. change the line in application.html.erb to use this

A

title>

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

interpolation

A

embed expressions into strings (even logic). process of inserting the result of an expression into a string literal (evaluate expression and insert)

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

comments in rails

A

lskdjfkd

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

puts

A

most commonely used ruby function to print a string. returns nil for nothing

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

’#{foo} bar’ output

A

”#{foo} bar”. ruby wont interpolate single quotes

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

usefulness of single quote literals

A

show exactly the characters you type

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

“foobar”.length

A

6

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

“foobar”.empty?

A

false

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

create if else statements: if s is nil, else if s is empty, else if s include “foo”

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

x = “foo”
y = “”
print out both if both are empty. print one if one of them is empty
print none if x is not empty

A

puts “both” if x.empty? && y.empty?
puts “one” if x.empty || y.empty?
puts
“x not empty” if !x.empty?

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

nil.to_s

A

””

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

nil.empty?

A

undefined method nil for nil:NilClass

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

“foo”.nil?

A

false

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

”“.nil?

A

false

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

nil.nil?

A

true

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

create similar statement to puts “x” if !x.empty? with “unless”

A

puts “x” unless x.empty?

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

def string_message(str = ‘ ‘). explain str

A

default argument str. you can call this method by saying “string_message” without arguments

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

“foo bar bax”.split

A

[“foo”, “bar”, “bax”]

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

“fooxbarxbazx”.split(‘x’)

A

[“foo”, “bar”, “baz]

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

a = [42, 8, 17]

a[-1]

A

17

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

name the methods you know you can do with:

x = “hello”

A
.length
.empty?
.nil?
.include?("foo")
.split
.split("l")
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

name the methods you know you can do with:

a = [5, 8, 6]

A
a[0] 5
a[-1] = 6
a.first
a.last
a.length
a.empty?
a.include?(42)
a.reverse
a.shuffle
a.sort! (changes a)
a.sort
a.push(6) [5, 8, 6, 6]
a
25
Q

create array with integers 0 through 9

A

0..9.to_a

26
Q

create array with all letters in alphabet

A

(‘a’..’z’).to_a

27
Q

turn “hello world my name is” into an array without split

A

a = %w[hello world my name is]

28
Q

explain:

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

A

calls the each method on the range (1..5) and passes it the block { |i| puts 2*i }. |i| is a block variable. each method can handle a block with a single variable and executes the block each value in the range

29
Q

when to use .each {} and .each do || end

A

.each {} for short one line code

.each do |i| end for multiple line

30
Q

range methods for:

1..5

A

.to_a
.each { |i| puts i}
.each do |i| puts i end
.map { |i| i**2} [1, 4, 9, 16, 25]

31
Q

test “should get home” do
get :home
assert_response :success
assert_select “title”, “ruby on rails”

A

body of the test is a block (from the keyword “do”).

and executes the block

32
Q

create array with all letters in alphabet and shuffle it and take first 8 elements and join them together

A

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

33
Q

hash define:

A

different storage format that arrays. objects within hash are given a key that points to them. (if order matters, use arrays)

34
Q

create empty hash

and add a key/value pair to it

A
user = {}
user["first_name"] = "yolo"
35
Q

define a hash with multiple key/value pairs in one line

A

user = {“first” => “yolo”, “last” => “lolo” }

36
Q

Symbols (important concept)

A

abstract references. symbols dont contain values or objects. they are used as consistent name within code.

37
Q

create hash with symbol and symbol from special case which ruby supports now

A

h1 = {:name => “michael”}
h2 = {name: “michael”}
h1 == h2
true

38
Q

create a nested hash and access the value within

A

params = {}
params[:user] = { name: “test”, email: “@.com”}
params[:user][name:]

39
Q

flash is a hash. iterate through it

A

flash.each do |key, value|

puts “key #{key.inspect} has value #{value.inspect}. .inspect returns string with a literal representation of the object

40
Q

puts “it worked”, “it worked”.inspect

A

it worked

“it worked”

41
Q

.insect relation to p

A

inspect to print an object is common enough that theres a shortcut for it, the p function

42
Q

stylesheet_link_tag ‘application’, media: ‘all’, ‘data-turbolinks-track’ => true (important to know)

A

in ruby, functions can have parentheses. but they are also optional
stylesheet_link_tag(…) same thing as the question. also the media argument looks like a hash. when hashes are the last arguments in a function, they curly braces are optional.
( {media: ‘all’, ‘data-turbolinks-track’ => true} is the same thing (we dont say data-turbolinks-track: because hyphens cant be used with symbols

43
Q

“fooar”.class

A

string

44
Q

s = String.new(“foobar”)

A

constructor for string

45
Q

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

A

array constructor

46
Q

h = Hash.new

A

{}

h[:foo]

47
Q

h = Hash.new(0)

A

{}
h[:foo]
0

48
Q

string

A

inheritance

49
Q

create a class called Word with a method called palindrome? which returns true if the words are the same in reverse

A
class Word
def palindrome?(string)
return string == string.reverse
end
end
w = Word.new
w.plaindrome?("level")
50
Q

create palindrome? with class Word inherating from string

A

class Word

51
Q

are ruby classes like String open to add more methods to them?

A

yes, i could create class String and add palindrome? method to it which will work for all string objects

52
Q

use .blank? and .empty? methods

A

” “.empty? false

“ “.blank? true (method provided with rails)

53
Q

create controller in ruby console for the controller we created

A

controller = StaticPagesController.new

constroller. class.superclass
controller. home

54
Q
class User
attr_accessor :name, :email (explain)
A

creates attribtue accessor.
this creates “getter” and “setter” methods that allow us to retrieve and assign @name and @email instance variables. (example.name = “example user’ and example.name are examples of the getter and setter only bcause of the attr_accessor)

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

default value is empty hash which defines a user and email with no values (nil).

56
Q

require ‘./example_user’

A

this is how you load the example_user code into the console. the . is unix for “current directory”.

57
Q

you created User with name and email instance variables. create it

A

user = User.new(name: “name”, email: “email);

58
Q

blocks in ruby

A

flexible construct that allow natural iteration over enumerable data structures.