Rails Tutorial Chapter 4 Notes

Memorandum of Chapter 4

environment

Rails 6.0.3 Ruby 2.6.3

table of contents

1 Array / Method 2 blocks 3 hashes 4 symbols 5 Decrypt CSS loading

1 Array / method

1.1 split method

It is possible to convert a character string to an array

#The default is to separate with spaces
>> "foo bar  baz".split 
=> ["foo", "bar", "baz"]

#You can specify the delimiter
>> "fooxbarxbaz".split('x')
=> ["foo", "bar", "baz"]

1.2 Try using -1 for the index

If you specify `` `-1``` for the index of the array, you can get the last element of the array.

>> a = [42, 8, 17]
=> [42, 8, 17]

>> a.last
=> 17
>> a[a.length - 1]
=> 17
>> a[-1]
=> 17

1.3 empty? method

You can check if it is empty

>> a = [42, 8, 17]
=> [42, 8, 17]

>> a.empty?
=> false

1.4 include? Method

You can check if the specified value is included

>> a = [42, 8, 17]
=> [42, 8, 17]

>> a.include?(42)
=> true

1.5 sort method

Sort the array in ascending order (smallest order)

>> a = [42, 8, 17]
=> [42, 8, 17]

>> a.sort
=> [8, 17, 42]

1.6 reverse method

Change in reverse order

>> a = [42, 8, 17]
=> [42, 8, 17]

>> a.reverse
=> [17, 8, 42]

1.7 shuffle method

Random sequence change

>> a = [42, 8, 17]
=> [42, 8, 17]

>> a.shuffle
=> [8, 42, 17]

1.8 Add `!` to each method

Change the value itself using the method

>> a = [42, 8, 17]
=> [42, 8, 17]

>> a.sort
=> [8, 17, 42]
>> a
=> [42, 8, 17] #a itself has not changed

>> a.sort!
=> [8, 17, 42]
>> a
=> [8, 17, 42] #a itself is also changed

1.9 push (<) method

Add an element to the end of the array

>> a.push(6)
=> [42, 8, 17, 6]

>> a << 7
=> [8, 42, 17, 6, 7]

1.10 join method

The reverse of the split method. Convert an array to a string

>> a = [42, 8, 17, 6, 7, "foo", "bar"]
=> [42, 8, 17, 6, 7, "foo", "bar"]

>> a.join
=> "4281767foobar" #Linking

>> a.join(',')  
=> "42,8,17,6,7,foo,bar" #Connect with a comma in between

1.11 to_a method

Convert to an array 0..9: Including 9

>> (0..9).to_a 
=> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

2 blocks

>> (1..5).each do |i|
?> puts 2*i
>> end
2
4
6
8
10
=> 10

If you enclose it in do ~ end like the above code, it becomes a block This seems to be the mainstream when there are multiple lines in the block.

3 hash

Hashes are different from arrays -A non-integer value can be used for the index (the index of the hash is called a key, and the corresponding value is the value) -The order of the elements in the hash is not guaranteed

4 symbols

Rails uses symbols rather than strings. Differences from strings are not enclosed in quotes and are preceded by a colon. It is common to use symbols as keys in hashes. Also, `=>` is called a hash rocket and can be rewritten as shown in the code below.

{ :name => "Test Name" }
{ name: "Test Name" }

5 Decrypt CSS loading

<%= stylesheet_link_tag 'application', media: 'all',
              'data-turbolinks-track': 'reload' %>

To understand this code ・ In Ruby, you don't have to use parentheses. ・ If the hash is the last argument of the method call, the curly braces can be omitted. It is necessary to grasp. Therefore,

<%= stylesheet_link_tag (
    'application', 
    { media: 'all', 'data-turbolinks-track': 'reload' }
) %>

And it turns out that there are two arguments.

The first element specifies the media type, The next element has the turbolinks feature turned on.

turbolinks: A library that speeds up page transitions. Added from Rails 4

Recommended Posts

Rails Tutorial Chapter 5 Notes
Rails Tutorial Chapter 4 Notes
Rails Tutorial Chapter 8 Notes
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 Memorandum (Chapter 3, 3.1)
Rails Tutorial Chapter 4 Learning
Rails Tutorial Chapter 1 Learning
Rails Tutorial Chapter 2 Learning
rails tutorial fighting notes Ⅲ
Rails Tutorial Memorandum (Chapter 3, 3.3.2)
rails tutorial
rails tutorial
rails tutorial
[Rails Tutorial Chapter 4] Rails-flavored Ruby
rails tutorial
rails tutorial
rails tutorial
[Rails Struggle/Rails Tutorial] Summary of Rails Tutorial Chapter 2
[Rails Tutorial Chapter 5] Create a layout
rails tutorial chapter 10 summary (for self-learning)
Chewing Rails Tutorial [Chapter 2 Toy Application]
Rails Tutorial (4th Edition) Memo Chapter 6
Rails tutorial test
Rails tutorial memorandum 1
Rails tutorial memorandum 2
Start Rails Tutorial
[Beginner] Rails Tutorial
html & rails notes
Rails Tutorial 6th Edition Learning Summary Chapter 10
Rails Tutorial 6th Edition Learning Summary Chapter 7
Rails Tutorial 6th Edition Learning Summary Chapter 4
Rails Tutorial 6th Edition Learning Summary Chapter 6
Rails Tutorial 6th Edition Learning Summary Chapter 5
Rails Tutorial 6th Edition Learning Summary Chapter 2
Rails Tutorial Chapter 0: Preliminary Basic Knowledge Learning 5
Rails Tutorial 6th Edition Learning Summary Chapter 3
Rails Tutorial 6th Edition Learning Summary Chapter 8
[Rails Struggle/Rails Tutorial] What you learned in Rails Tutorial Chapter 6
Chapter 4 Rails Flavored Ruby
[Rails Struggle/Rails Tutorial] What you learned in Rails Tutorial Chapter 3
Rails Tutorial cheat sheet
Rails Tutorial Chapter 1 From Zero to Deployment [Try]
[Rails] Learning with Rails tutorial
Chewing Rails Tutorial [Chapter 3 Creating Almost Static Pages]
[Rails tutorial] A memorandum of "Chapter 11 Account Activation"
Resolve Gem :: FilePermissionError when running gem install rails (Rails Tutorial Chapter 1)
[Ruby on Rails Tutorial] Error in the test in Chapter 3
Rails Tutorial 4th Edition: Chapter 1 From Zero to Deployment
Resolve ActiveRecord :: NoDatabaseError when doing rails test (Rails tutorial Chapter 3)
Ruby on Rails Tutorial Troublesome notes when running on Windows
11.1 AccountActivations Resources: Rails Tutorial Notes-Chapter 11
Rails Tutorial Records and Memorandum # 0
I tried Rails beginner [Chapter 1]
Rails Tutorial (4th Edition) Summary
I tried Rails beginner [Chapter 2]