Chapter 4 Flashcards
true %>
build in rails function (stylesheet_link_tag) to include application.css for all media types (including computer screens and printers.
helper functions
creation of new rails functions
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
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
using full_title method, create a new title tag to use it:
steps to make home page with no custom page title
update home page to remove the provide function. update the test assert to test this
#
comments in ruby
string literal
zero or more characters grouped in quotes
concatenate two strings
“foo” + “bar”
“foobar”
interpolate a string and define meaning
first_name = “sam”
“#{first_name} Erd”
“Sam Erd”. interpolation is using a placeholderto insert a certain variable
printing a string
puts which appends a new line
nil
literally nothing
\n
new line
difference between ‘’ and “”
nothing, there identical, except ruby wont interpolate into single-quoted strings
is everything in ruby an object?
yes. even nil is an object.
what is an object?
object is a model of something with behavior and functions and attributes. place to store data about an entity.
empty?
method returns true or false
if s is nil, return “nil”, else if s is empty, return “empty”, else if, s includes “foo”, return “foo”
if s.nil? "nil" elsif s.empty? "empty" elsif s.include?("foo") "foo" else "lol" end
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
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?
puts two variables if neither is empty
puts “#{x} #{y}” if !x.empty? && !y.empty?
.to_s
convert virtually any object to a string.
nil.to_s
””
nil.empty?
error
nil.to_s.empty?
true
”“.nil?
false
nil.nil?
true
puts a string using interpolation unless its empty
puts “#{foo}” unless foo.empty?
!!nil
false
!!0
true
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
return string if its empty
return “empty” if str.empty?
last return in a function is unnecessary why?
returned regardless of return keyword
module ApplicationHelpter
gives us a way to package together related methods which can be then mixed in to Ruby classes using “include”
“foo bar yo” split this string into an array
“foo bar yo”.split
[“foo”, “bar”, “yo”]