Rails Tutorial Chapter 4 Learning

Where I thought (.´ ・ ω ・) in Chapter 4 of Rails Tutorial

I think that it is because it is output with prerequisite knowledge and review, I haven't really thought about this chapter (.´ ・ ω ・). The video and text were very easy to understand.

Rails Chapter 4 Review of simple contents

Chapter 4 is Rails-flavored Ruby. This is the first time for me to understand object-oriented programming. I also learned in Python from Progate and Ruby from TECH :: CAMP. .. .. (゜-゜) Hmm, I was wondering if object-oriented programming is delicious.

Well explained ... All things are objects. One object for people, one object for cars There is a person named John. John belongs to a person. People have factors such as occupation, gender, and age. .. .. It feels like the code is written according to this content (.´ ・ ω ・)?

Explaining object orientation in my words ...

"String"there is.
When written in object orientation,
"12345".length #You can use the method to count characters.
"12345".to_i   #You can use a method to convert a string to an integer.

.length method.to_What can I use even though i method is not defined??
⇒Because the answer is written in an object-oriented manner.

yet,(.. ´ ・ ω ・)Hmm?I'm feeling

Actually"String"と書けばStringと認識されていますが、
String.new("String")Is an abbreviation for the code.
String(String)Class object"String"It means to make.

Character string in advance(String)There are various methods in the class.
String.new("")Since the object created in is a string object, you can use the predefined methods.

In a string class.to_i method is prepared, so
Also the newly generated string.to_It means that you can use various methods such as i.

The biggest advantage of object orientation is
By always generating a character string as a character string object.to_It is possible to share methods such as i.('ω')No
If you add a new method to the string class, it can be used with all string objects.
When I learned at Progate(.. ´ ・ ω ・)Hmm?It was, but now I find it convenient(´-ω-`)

Classes and inheritance

"String"Belongs to the String class.
To see where it belongs.You can use the class method.

s = "String"

s.class
⇒ String

In fact, the String class also belongs to the Object class.
>> s.class.superclass             # superclass :Examine the parent class
=> Object

The Object class also belongs to a higher class.
>> s.class.superclass.superclass 
=> BasicObject

Does BasicObject belong to a higher class? The answer was no. It means that nil does not exist.
>> s.class.superclass.superclass.superclass
=> nil

The reason why I am doing this is to take over the function.('ω')No
Under the Object class, String(String)・ Integer(integer)・ Array(Array)There are classes and so on.
Arrays, integers, strings,.You can use the length method, right?
It's hard to add each of the three.
So if you add it to Object and make it available in lower classes, you can save time and effort.

Inheriting a method defined in a higher class to a lower method in this way is called "inheritance".('ω')No

#class <name of the class>You can define a new class with.
>> class Word < String             # <String can inherit String class('ω')No
>>   #Returns true if the string is a palindrome
>>   def palindrome?        #You can define a new method with def.
>>     self == self.reverse        #self represents the string itself
>>   end
>> end
=> :palindrome?

Ruby basics

$ rails console

#Integer addition
>> 17 + 42
=> 59

#String concatenation
>> "foo" + "bar"    #String concatenation
=> "foobar"

#Variable assignment
>> first_name = "Michael"
=> "Michael"

#Expression expansion of character strings This is convenient('ω')No, I also use it in Rails.
>> "#{first_name} Hartl"
=> "Michael Hartl"

#Expression expansion('ω')No
>> first_name = "Michael"
=> "Michael"
>> last_name = "Hartl"
=> "Hartl"
>> first_name + " " + last_name    #Combine with a space between the last name and the first name
=> "Michael Hartl"
>> "#{first_name} #{last_name}"    #Join using expression expansion(Exactly the same as above)
=> "Michael Hartl"

#String output(puts)
>> puts "foo"     #Output a string
foo
=> nil #The return value is nil "nothing"

#String output(print)
>> print "foo"    #Screen output of character string(Same as puts but no line breaks)
foo=> nil

>> print "foo\n"  # \n is a line break. This will have the same output as puts.
foo
=> nil

#''With single quotes""Double quotation
>> '#{first_name} #{last_name}'    # ''Cannot expand the expression.
=> "/#{first_name} #{last_name}"

>> "#{first_name} #{last_name}"    # ""Can be expanded.
=> "Michael Hartl"

# empty?Method
>> "foobar".empty?
=> false
>> "".empty?
=> true

#Conditional branch(if)・.include?("String")・.nil?Method
>> if s.nil?
>>   "The variable is nil"
>> elsif s.empty?
>>   "The string is empty"
>> elsif s.include?("foo")
>>   "The string includes 'foo'"
>> end
=> "The string includes 'foo'"


#Van!Van!Forced logical value(True/False)Output with
>> !!nil #Ruby object is false only for nil
=> false

>> !!0  #All other Ruby objects are true
=> true

Is this about half the content? There is quite a lot of volume. Important contents such as arrays and hashes follow after this ... I wonder if it's copyrightable if I write everything, so if I write about symbols, it's over ('ω')

About symbols, integers, and strings

#This is a labeled array called a hash.
user1 = { "name"=>"test_user", "email" => "[email protected]" }
user2 = { "name"=>"test_user", "email" => "[email protected]" }

user1 == user2
=> false

Why is it false even though the contents are the same?('ω')No
Actually object_The thing called id is different, so please check that it is different.
user1.object_id 
user2.object_id

This object_The id is like the place where the data is stored.
Try it with a string as well. Object every time you run_The id will change.
"name".object_id
"name".object_id
"name".object_id

Next is the number. How about the numbers? The numbers are always the same object_It becomes id.
1.object_id
1.object_id
1.object_id

Finally ... a symbol. Symbols are objects as well as numbers_The id doesn't change.
:name #:+A character string is called a symbol, but object_Think of it as something labeled with id.

The reason for doing this is that it is faster than using strings.('ω')No
So the hash label name prefers symbols to strings.

◎ user = { :name=> "test_user" , :email => "[email protected]" }
△ user = { "name"=>"test_user", "email" => "[email protected]" }

It can be written even more easily.
user = { :name=> "test_user" , :email => "[email protected]" }
user = { name: "test_user", email: "[email protected]" }

Custom helper

If you define the method yourself, write it in a file called helper ('ω')

app/helpers/application_helper.rb


module ApplicationHelper

  #Returns the full title per page.
  def full_title(page_title = '')
    base_title = "Ruby on Rails Tutorial Sample App"
    if page_title.empty?
      base_title
    else
      page_title + " | " + base_title
    end
  end
end

Impressions

Chapter 4 is very rich and interesting. You need to know Ruby to define various methods by yourself. There are many methods, but I only remember a few. After learning Rails, you have to learn Ruby properly ... Hmm, the tip is long and long (^ ω ^) ...

Recommended Posts

Rails Tutorial Chapter 4 Learning
Rails Tutorial Chapter 1 Learning
Rails Tutorial Chapter 2 Learning
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 5 Notes
[Rails] Learning with Rails tutorial
Rails Tutorial Memorandum (Chapter 3, 3.1)
Rails Tutorial Chapter 8 Notes
Rails Tutorial Memorandum (Chapter 3, 3.3.2)
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 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 learning day 3
Rails tutorial memorandum 2
Rails learning day 4
Rails learning day 2
Start Rails Tutorial
[Beginner] Rails Tutorial
rails learning day 1
[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 day 2 part 2
Rails learning day 1 part 3
Rails learning day 3 part 2
Chewing Rails Tutorial [Chapter 3 Creating Almost Static Pages]
[Rails tutorial] A memorandum of "Chapter 11 Account Activation"
Rails learning Day 1 Part 2
rails tutorial fighting notes Ⅲ
Resolve Gem :: FilePermissionError when running gem install rails (Rails Tutorial Chapter 1)
[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
Rails Tutorial 4th Edition: Chapter 1 From Zero to Deployment