Chapter 4 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
create array with integers 0 through 9
0..9.to_a
26
create array with all letters in alphabet
('a'..'z').to_a
27
turn "hello world my name is" into an array without split
a = %w[hello world my name is]
28
explain: | (1..5).each { |i| puts 2*i }
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
when to use .each {} and .each do || end
.each {} for short one line code | .each do |i| end for multiple line
30
range methods for: | 1..5
.to_a .each { |i| puts i} .each do |i| puts i end .map { |i| i**2} [1, 4, 9, 16, 25]
31
test "should get home" do get :home assert_response :success assert_select "title", "ruby on rails"
body of the test is a block (from the keyword "do"). | and executes the block
32
create array with all letters in alphabet and shuffle it and take first 8 elements and join them together
('a'..'z').to_a.shuffle[0..7].join
33
hash define:
different storage format that arrays. objects within hash are given a key that points to them. (if order matters, use arrays)
34
create empty hash | and add a key/value pair to it
``` user = {} user["first_name"] = "yolo" ```
35
define a hash with multiple key/value pairs in one line
user = {"first" => "yolo", "last" => "lolo" }
36
Symbols (important concept)
abstract references. symbols dont contain values or objects. they are used as consistent name within code.
37
create hash with symbol and symbol from special case which ruby supports now
h1 = {:name => "michael"} h2 = {name: "michael"} h1 == h2 true
38
create a nested hash and access the value within
params = {} params[:user] = { name: "test", email: "@.com"} params[:user][name:]
39
flash is a hash. iterate through it
flash.each do |key, value| | puts "key #{key.inspect} has value #{value.inspect}. .inspect returns string with a literal representation of the object
40
puts "it worked", "it worked".inspect
it worked | "it worked"
41
.insect relation to p
inspect to print an object is common enough that theres a shortcut for it, the p function
42
stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true (important to know)
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
"fooar".class
string
44
s = String.new("foobar")
constructor for string
45
a = Array.new([1,3,2])
array constructor
46
h = Hash.new
{} | h[:foo]
47
h = Hash.new(0)
{} h[:foo] 0
48
string
inheritance
49
create a class called Word with a method called palindrome? which returns true if the words are the same in reverse
``` class Word def palindrome?(string) return string == string.reverse end end w = Word.new w.plaindrome?("level") ```
50
create palindrome? with class Word inherating from string
class Word
51
are ruby classes like String open to add more methods to them?
yes, i could create class String and add palindrome? method to it which will work for all string objects
52
use .blank? and .empty? methods
" ".empty? false | " ".blank? true (method provided with rails)
53
create controller in ruby console for the controller we created
controller = StaticPagesController.new constroller. class.superclass controller. home
54
``` class User attr_accessor :name, :email (explain) ```
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
``` def initialize(attributes = {}) @name = attributes[:name] @email = attributes[:email] end ```
default value is empty hash which defines a user and email with no values (nil).
56
require './example_user'
this is how you load the example_user code into the console. the . is unix for "current directory".
57
you created User with name and email instance variables. create it
user = User.new(name: "name", email: "email);
58
blocks in ruby
flexible construct that allow natural iteration over enumerable data structures.