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.
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 (.´ ・ ω ・)?
"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(´-ω-`)
"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?
$ 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 ('ω')
#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]" }
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
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