Chapter 4 Flashcards
true %>
includes application.css for all media types(computer screens and printers ect). to rails developer, this line looks simple.
define full_title helper method. returns base title if no page title is defined
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
you created full_title helper method. change the line in application.html.erb to use this
title>
interpolation
embed expressions into strings (even logic). process of inserting the result of an expression into a string literal (evaluate expression and insert)
comments in rails
lskdjfkd
puts
most commonely used ruby function to print a string. returns nil for nothing
’#{foo} bar’ output
”#{foo} bar”. ruby wont interpolate single quotes
usefulness of single quote literals
show exactly the characters you type
“foobar”.length
6
“foobar”.empty?
false
create if else statements: if s is nil, else if s is empty, else if s include “foo”
if s.nil? "nil" elsif s.empty? "empty" elsif s.include?("foo") "includes foo" end
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
puts “both” if x.empty? && y.empty?
puts “one” if x.empty || y.empty?
puts
“x not empty” if !x.empty?
nil.to_s
””
nil.empty?
undefined method nil for nil:NilClass
“foo”.nil?
false
”“.nil?
false
nil.nil?
true
create similar statement to puts “x” if !x.empty? with “unless”
puts “x” unless x.empty?
def string_message(str = ‘ ‘). explain str
default argument str. you can call this method by saying “string_message” without arguments
“foo bar bax”.split
[“foo”, “bar”, “bax”]
“fooxbarxbazx”.split(‘x’)
[“foo”, “bar”, “baz]
a = [42, 8, 17]
a[-1]
17
name the methods you know you can do with:
x = “hello”
.length .empty? .nil? .include?("foo") .split .split("l")
name the methods you know you can do with:
a = [5, 8, 6]
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
create array with integers 0 through 9
0..9.to_a
create array with all letters in alphabet
(‘a’..’z’).to_a
turn “hello world my name is” into an array without split
a = %w[hello world my name is]
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
when to use .each {} and .each do || end
.each {} for short one line code
.each do |i| end for multiple line
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]
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
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
hash define:
different storage format that arrays. objects within hash are given a key that points to them. (if order matters, use arrays)
create empty hash
and add a key/value pair to it
user = {} user["first_name"] = "yolo"
define a hash with multiple key/value pairs in one line
user = {“first” => “yolo”, “last” => “lolo” }
Symbols (important concept)
abstract references. symbols dont contain values or objects. they are used as consistent name within code.
create hash with symbol and symbol from special case which ruby supports now
h1 = {:name => “michael”}
h2 = {name: “michael”}
h1 == h2
true
create a nested hash and access the value within
params = {}
params[:user] = { name: “test”, email: “@.com”}
params[:user][name:]
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
puts “it worked”, “it worked”.inspect
it worked
“it worked”
.insect relation to p
inspect to print an object is common enough that theres a shortcut for it, the p function
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
“fooar”.class
string
s = String.new(“foobar”)
constructor for string
a = Array.new([1,3,2])
array constructor
h = Hash.new
{}
h[:foo]
h = Hash.new(0)
{}
h[:foo]
0
string
inheritance
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")
create palindrome? with class Word inherating from string
class Word
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
use .blank? and .empty? methods
” “.empty? false
“ “.blank? true (method provided with rails)
create controller in ruby console for the controller we created
controller = StaticPagesController.new
constroller. class.superclass
controller. home
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)
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).
require ‘./example_user’
this is how you load the example_user code into the console. the . is unix for “current directory”.
you created User with name and email instance variables. create it
user = User.new(name: “name”, email: “email);
blocks in ruby
flexible construct that allow natural iteration over enumerable data structures.