■ Helper What to call when you want to do a little processing in the view. The entity is module. Defined in app / helpers. Call with <% = ~%> when you want to use it.
■ Built-in helper A function built into Rails so that it can be handled as a method when processing a certain operation. It can be used without defining it in helpers.
[Practice]
Let's create a character string for an address like "Shinjuku-ku, Tokyo" using the variables and formula expansion created earlier. Use puts for output. puts prefecture + "prefecture" + city + "city"
What happens if you replace the string you replaced with a tab with a double quote to a single quote? Try it
■ Object In Ruby, everything is an object.
You can ask a question in a string, and the string will answer that question. If you ask the question (method) "empty?", It will answer whether the string is empty.
■ Method chain Connect to_s and empty with a feeling like "nil.to_s.empty?".
[Practice]
Use the reverse method to find out what happens when you read the string "racecar" in reverse. racecar.reverse
What is the result of running Listing 4.9? What happens if I assign the string "onomatopoeia" to the variable s? Tip: Use the up arrow (or Ctrl-P command) to replay the previously used command. It's convenient because you don't have to type all the commands from scratch. ) puts "It's a palindrome!" if s == s.reverse s="onomatopoeia" puts "It's a palindrome!" if s == s.reverse
[Practice]
Try using the methods defined above to see if “racecar” and “onomatopoeia” are palindromes. If the result is that the first is a palindrome and the second is not a palindrome, it is a success. palindrome_tester (s) s with racecar, onomatopoeia It is OK if you write each.
[Practice]
Now try substituting the result (string) of concatenating the elements of variable a into variable s. s = a.join
Divide the variable s with a space and then concatenate it again to make a string (hint: you can do it on one line using the method chain). Use the method to check the palindrome used in Listing 4.10 to make sure that the variable s is not a palindrome (currently yet). Use the downcase method to make sure s.downcase is a palindrome. s = s..split(" ") def palindrome_tester(s) palindrome_tester(s.split.join.downcase)
Create a range object from a to z and try extracting the 7th element. In the same way, try to extract the 7th element from the back. (Hint: don't forget to convert the range object to an array) a=('a'..'z').to_a a[6] a[-7]
■ map method The processing in the block is repeated for the number of elements of the array, and the resulting array is returned.
■ do method Use it together with test to execute all the processing up to end.
[Practice] abridgement
■ Hash Hash = associative array
■ Symbol It's faster to use symbols. In a composite array user = { "name" => "Michael Hartl", "email" => "[email protected]" } user = { :name => "Michael Hartl", :email => "[email protected]" } user = { name : "Michael Hartl", email : "[email protected]" } Looks the same.
[Practice] abridgement
[Practice] abridgement
■superclass Everything belongs to some class. Since rails makes all of them belong, it is possible to develop applications automatically.
[Practice] abridgement
[Practice] abridgement
[Practice] abridgement
[Practice] abridgement
I am amazed at my low motivation. Let's come back after doing Chapter 5.
Recommended Posts