Chapter 4 Rails Flavored Ruby

4.1 Motivation

■ 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.

4.2 Strings and methods

[Practice]

    1. Substitute the appropriate city / ward / town / village in the city variable and the appropriate prefecture in the prefecture variable. city = "Yokohama" prefecture = "Kanagawa"
  1. 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"

    1. Try replacing the half-width space between the above strings with a tab. (Hint: Same as newline character, tab is also special character) Try it
  2. 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.

  1. Have an identity
  2. Receive a message
  3. Have an internal state An object is one that responds to a message (at any time).

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]

    1. What is the length of the "racecar" string? Try using the length method to find out. racecar.length
  1. Use the reverse method to find out what happens when you read the string "racecar" in reverse. racecar.reverse

    1. Assign "racecar" to the variable s. Then use the comparison operator (==) to see if the values of the variables s and s.reverse are the same. s=racecar s == s.reverse
  2. 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]

    1. Try replacing the FILL_IN part of Listing 4.10 with the appropriate code and defining a method that checks for palindromes. Tip: See the comparison method in Listing 4.9. if s==s.reverse
  1. 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.

    1. Try calling the nil? method on palindrome_tester ("racecar") and see if the return value is nil (that is, make sure the result of calling nil? Is true). This method chain means that the nil? method receives the return value in Listing 4.10 and returns the result. →palindrome_tester("racecar").nil?

4.3 Other data structures

[Practice]

    1. Try splitting the string “A man, a plan, a canal, Panama” into an array with “,” and assigning it to the variable a. a = "A man, a plan, a canal, Panama".split(",")
  1. Now try substituting the result (string) of concatenating the elements of variable a into variable s. s = a.join

  2. 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)

  3. 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

4.4 Classes in Ruby

[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

4.5 Finally

I am amazed at my low motivation. Let's come back after doing Chapter 5.

Recommended Posts

Chapter 4 Rails Flavored Ruby
[Rails Tutorial Chapter 4] Rails-flavored Ruby
rails tutorial Chapter 6
rails tutorial Chapter 1
rails tutorial Chapter 7
rails tutorial Chapter 5
rails tutorial Chapter 10
rails tutorial Chapter 9
rails tutorial Chapter 8
Rails Tutorial Chapter 5 Notes
Rails Tutorial Chapter 10 Notes
Rails Tutorial Chapter 3 Notes
Rails Tutorial Chapter 3 Learning
Ruby on Rails Elementary
Ruby on Rails basics
Rails Tutorial Memorandum (Chapter 3, 3.1)
Rails Tutorial Chapter 4 Notes
Rails Tutorial Chapter 4 Learning
Ruby On Rails Association
Rails Tutorial Chapter 1 Learning
Rails Tutorial Chapter 2 Learning
Ruby, Rails finally installed
Rails Tutorial Chapter 8 Notes
Rails Tutorial Memorandum (Chapter 3, 3.3.2)
[Ruby on Rails Tutorial] Error in the test in Chapter 3
Ruby on Rails5 Quick Learning Practice Guide 5.2 Compatible Chapter2
Ruby on Rails5 Quick Learning Practice Guide 5.2 Compatible Chapter3
Ruby on rails learning record -2020.10.03
Rails engineer environment construction ruby2.7.1
Portfolio creation Ruby on Rails
Rails environment construction Rails5.2.1 ruby2.5.1 Catalina
Ruby on rails learning record -2020.10.04
[Ruby on Rails] Debug (binding.pry)
Ruby on rails learning record -2020.10.05
Ruby on rails learning record -2020.10.09
Ruby on Rails config configuration
Ruby on Rails basic learning ①
[Ruby on Rails] about has_secure_password
Ruby on rails learning record-2020.10.07 ②
I tried Rails beginner [Chapter 1]
Commentary on partial! --Ruby on Rails
Ruby on rails learning record-2020.10.07 ①
Cancel Ruby on Rails migration
I tried Rails beginner [Chapter 2]
Ruby on rails learning record -2020.10.06
Ruby on Rails validation summary
Ruby on Rails Basic Memorandum
[Rails Struggle/Rails Tutorial] Summary of Rails Tutorial Chapter 2
[Ruby on Rails] Read try (: [],: key)
[Ruby on Rails] yarn install --check-files
Ruby on Rails variable, constant summary
Installing Ruby + Rails on Ubuntu 18.04 (rbenv)
[Ruby on Rails] Introduced paging function
Basic knowledge of Ruby on Rails
Progate Ruby on Rails5 Looking Back
[Rails Tutorial Chapter 5] Create a layout
How to use Ruby on Rails
rails tutorial chapter 10 summary (for self-learning)
[Ruby on Rails] Add / Remove Columns
Ruby on Rails Japanese-English support i18n
(Ruby on Rails6) "Erase" posted content