Memorandum of Chapter 4
Rails 6.0.3 Ruby 2.6.3
1 Array / Method 2 blocks 3 hashes 4 symbols 5 Decrypt CSS loading
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"]
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
You can check if it is empty
>> a = [42, 8, 17]
=> [42, 8, 17]
>> a.empty?
=> false
You can check if the specified value is included
>> a = [42, 8, 17]
=> [42, 8, 17]
>> a.include?(42)
=> true
Sort the array in ascending order (smallest order)
>> a = [42, 8, 17]
=> [42, 8, 17]
>> a.sort
=> [8, 17, 42]
Change in reverse order
>> a = [42, 8, 17]
=> [42, 8, 17]
>> a.reverse
=> [17, 8, 42]
Random sequence change
>> a = [42, 8, 17]
=> [42, 8, 17]
>> a.shuffle
=> [8, 42, 17]
`!`
to each methodChange 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
Add an element to the end of the array
>> a.push(6)
=> [42, 8, 17, 6]
>> a << 7
=> [8, 42, 17, 6, 7]
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
Convert to an array 0..9: Including 9
>> (0..9).to_a
=> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>> (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.
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
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" }
<%= 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