[Ruby] Chapter 4. Rails-flavored Ruby

3 minute read

4.1 Motivation

■ Helper What you call when you want to do a little processing in the view. The entity is module. Define in app/helpers. When you want to use it, call it with <%= ~ %>.

■ Built-in helper A function built into Rails so that it can be handled as a method when processing a certain operation. You can use it without defining it with helpers.

4.2 Strings and methods


  1. Substitute the appropriate municipality for the city variable and the appropriate prefecture for the precision variable. city = “Yokohama” prefecture = “Kanagawa”

  2. Let’s create a character string for an address like “Shinjuku-ku, Tokyo” using the variables and expression expansion that we created earlier. Use puts for output. puts prefecture+ “prefecture” + city + “city”

  3. Try replacing the half-width spaces between the strings above with tabs. (Hint: tab is a special character as well as newline) Try it

  4. What happens if I replace the string replaced with tab with double quote instead of single quote? Try it

■ Object In Ruby, everything is an object.

  1. have an identity
  2. Receive the message
  3. Having internal state An object is one that responds to messages (at any time).

You can ask a string for a question, and the string will answer that question. If you ask the question (method) “empty?”, it will answer if the string is empty.

■ Method chain Connect to_s and empty like “nil.to_s.empty?”.


  1. What is the length of the “racecar” string? Check it out using the length method. racecar.length

  2. Use the reverse method to find out what happens when you read the “racecar” string in reverse. racecar.reverse

  3. Substitute “racecar” for the variable s. Then use the comparison operator (==) to find out if the variables s and s.reverse have the same value. s=racecar s == s.reverse

  4. What is the result of running Listing 4.9? What happens when you assign the string “onomatopoeia” to the variable s? Tip: Use the up arrow (or Ctrl-P command) to re-execute your previous command. It is convenient because you don’t have to type all commands from scratch. ) puts “It’s a palindrome!” if s == s.reverse s=”onomatopoeia” puts “It’s a palindrome!” if s == s.reverse


  1. Try replacing the FILL_IN part in Listing 4.10 with the appropriate code and defining a method to check for palindromes. Tip: See the comparison in Listing 4.9. if s==s.reverse

  2. Use the method defined above to see if “racecar” and “onomatopoeia” are palindromes. The first is a palindrome, and the second is not a palindrome. racecar, onomatopoeia on s of palindrome_tester(s) You can write each of them.

  3. Try calling the nil? method on palindrome_tester(“racecar”) and see if the return value is nil (ie make sure the result of calling nil? is true). This method chain means that the nil? method receives the return value from Listing 4.10 and returns the result. →palindrome_tester(“racecar”).nil?

4.3 Other data structures


  1. Try dividing the character string “A man, a plan, a canal, Panama” by “,” into an array and assigning it to the variable a. a = “A man, a plan, a canal, Panama”.split(“,”)

  2. Now, try assigning the result (string) of concatenating the elements of variable a to variable s. s = a.join

  3. Divide the variable s by half-width space and then concatenate it again to make a string (Hint: You can even use a single line with the method chain). Use the palindrome checking method used in Listing 4.10 to make sure that the variable s is (currently) not a palindrome. Use the downcase method to make sure that s.downcase is a palindrome. s = s..split(“ “) def palindrome_tester(s) palindrome_tester(s.split.join.downcase)

  4. Try creating a range object from a to z and taking out the 7th element. In the same way, try to get 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 array elements, and the resulting array is returned.

■ do method Use with test to execute all processes 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]”} Will be the same.

[Practice] abridgement

4.4 Class in Ruby

[Practice] abridgement

■ superclass Everything belongs to some class. Since rails belongs to us, we can develop applications automatically.

[Practice] abridgement

[Practice] abridgement

[Practice] abridgement

[Practice] abridgement

4.5 Finally

I was surprised at my low motivation. Come back after doing Chapter 5.