rails learning day 1

Ruby on Rails 5 Quick Learning Practice Guide chapter1

1-1-6 Classes and Instances (p.6)

・ Relationship between class and instance The class is a parent and the instances are children, and there are many instances in the class. Untitled-2.jpg To give an example, Mr. A (instance), Mr. B (instance), Mr. C (instance), and Mr. D (instance) exist in the outline (class) of human beings.

1-2-5 Difference between local variable and instance variable

type Feature
Local variables An ad hoc temporary variable. Can only be used within defined methods
Instance variables Variables held by the object. Once defined within an instance, it can be used for any method that is not in that instance

Example)

class MyClass
 def method_1
  @number=100
 end

 def method_2
  @number  #Other methods(method_1)Defined in@another method for number(method_2)Available at
 end
end

1-2-7 How to use (attr_accessor) to easily define getters and setters

If you want to use instance variables such as @name properly, you have to do two things:

  1. Define a method to assign the name of the created information to @name so that it can be used.
  2. Define a name method to display @name
class User
 def name=(name) #1.
  @name = name
 end

 def name #2.
  @name 
 end

The method that can use @name is completed only after coming here. ... but it's annoying.

class User
 attr_accessor :name
end

By using attr_accessor, it was refreshed at once.

1-2-8 Have a place to live and an e-mail address (specific usage of attr_accessor)

Various attributes can be attached to attr_accessor

class User
 atte_accessor :name, :address, :email
end

If you want to use each attribute, do as follows

user = User.new
user.name = "Ryohei Odai"
user.address = "Tokyo"

def profile  #I made a method to display the name and origin
 "#{name}(#{address})"
end

user.profile #Executed the profile method that displays the user's name and origin together
=> "Ryohei Odai(Tokyo)"

You can enter and call user information using name and address.

1-3-4-2 unless it branches when it does not apply (how to use unless)

if executes the process when the result of the condition is true, but unless executes the process when the result of the condition is false. (Reverse if)

1-3-4-3 postfix if

The usual if is attached to the back. If etc. cannot be attached and acts on only one line

puts 'Good morning' if true  #Output

puts 'Thank you for your hard work' if false  #Not output

The above code will be "output puts if the result is true" Since the result is true, "Good morning" is output.

1-3-6 hash

If you want to get the value you want in an ordinary array, you have to specify the number (number). This becomes impossible as the amount of array information increases. Therefore, we use a hash and give each value something like a nickname (key). By doing so, if you enter a nickname, the value associated with it will automatically appear.

 jinnkou ={ tokyo: 13636222, kanagawa: 9145572 }
         #{Nickname (key):Tokyo population,Nickname (key):Kanagawa population}

puts jinnkou[:tokyo]
=> 13636222

Put: in [] and put the nickname (key)

1-4-1 initialize Whenever you execute "○○ .new" in Ruby, initialize in the ○○ class is executed. Allows you to enter the corresponding value in a table column For example, when putting new information in the User table with name, address, email columns If you write initialize (name, address, email), you will perform operations such as "the first in parentheses is the name, the second is the address, and the third is the email."

class User #User.You can use initialize when new
 def initialize(name, address, email)
   @name = name
   @address = address
   @email = email
 end
end
user = User.new("Neiko Oba","Tokyo","nil")
                 # ||       ||      ||
  #initialize(  name   ,address, email)

1-4-2 Restrict method invocation (private)

In terms of security, if anyone can use the method, it will cause problems such as leakage of personal information, so use the private method to prevent the method from being used from the outside.

1-5-2 Module commonality (Mix-in)

Make small-scale methods (only one process or the process itself easy) into a group as a module so that it can be used in various classes.

#Module summarizing the price
module PriceHolder
 def total_price
  price*Tax.rate
 end
end

class Product
 include PriceHolder
 attr_accessor :price
end

class OrderedItem
 include PriceHolder
 attr_accessor :unit_price, :volume

#Unit price excluding tax*quantity
 def price
  unit_price*volume
 end
end

The process of multiplying the price by the consumption tax rate to get the return value is used as the PriceHolder module, and the PriceHolder module is used for each class. Use include when using modules.

1-7-1 nil guard

number ||=10

If there is a number, number is used, otherwise number is used by substituting 10 for number.

1-7-2 Bocchi operator &.

For example, if you have a User table but type object.name, you will get an error because there is no object table. However, if you use the bocchi operator object & .name, if there is no table, nil will be returned as the return value instead of an error.

1-7-3% notation

ary1 = ['apple', 'banana' ,'orange']
# %If you use the notation
ary1 = %w[apple banana orange]

If you use the% notation, you can omit "" and so on.

1-7-4 Extract only specific attributes from each element of the array (map method)

class User
 attr_accessor :name, :address
end

user1 = User.new(name:'Neiko Oba', address:'Tokyo')
user2 = User.new(name:'Miyuki Koshiba', address:'Chiba')
user3 = User.new(name:'Ryohei Odai', address:'Kanagawa Prefecture')

users = [user1,user2.user3]

At this time, if you want an array with only the names of each user, use the map method.

names = users.map(&:name)
=>["Neiko Oba","Miyuki Koshiba","Ryohei Odai"]

In this way, if you use the original array .map (&: desired information), you can create an array of only the information you want.

Other notes that I learned

1.require: Used when reading the source code (user.rb in this case)

> require './user.rb'
  1. Timing to use return

The value returned when the method is executed is called the "return value". Basically, when a method is executed, it gives some return value, but it gives a return value when all the methods have been executed. If you want a return value in the middle of a method, use return to get the return value.

Recommended Posts

Rails learning day 4
Rails learning day 2
rails learning day 1
Rails learning Day 1 Part 2
Programming learning day 3
java learning day 2
java learning day 1
Rails Tutorial Chapter 3 Learning
[Rails] Learning with Rails tutorial
Rails Tutorial Chapter 4 Learning
Rails Tutorial Chapter 1 Learning
Rails Tutorial Chapter 2 Learning
Ruby on rails learning record -2020.10.04
Ruby on rails learning record -2020.10.05
4th day of java learning
Ruby on Rails basic learning ①
Ruby on rails learning record-2020.10.07 ②
Ruby on rails learning record-2020.10.07 ①
Ruby on rails learning record -2020.10.06
rails learning rake task, cron, whenever
[Rails g.error]
Rails Tutorial 6th Edition Learning Summary Chapter 10
Java learning (0)
Ruby learning 4
Rails Tutorial 6th Edition Learning Summary Chapter 7
Rails basics
Rails API
Rails migration
Rails Tutorial 6th Edition Learning Summary Chapter 4
[Rails] first_or_initialize
Java Day 2018
rails tutorial
Rails Tutorial 6th Edition Learning Summary Chapter 9
Rails Tutorial 6th Edition Learning Summary Chapter 6
About Rails 6
Servlet learning
Ruby learning 3
Rails foundation
Rails Tutorial 6th Edition Learning Summary Chapter 5
Rails memorandum
rails tutorial
rails tutorial
Learning output ~ 11/3 ~
Rails Tutorial 6th Edition Learning Summary Chapter 2
Rails Tutorial Chapter 0: Preliminary Basic Knowledge Learning 5
Ruby learning 2
Maven learning
[Rails] devise
Ruby learning 6
rails tutorial
Rails Tutorial 6th Edition Learning Summary Chapter 3
rails tutorial
Learning output
Rails Tips
rails method
rails tutorial
Ruby learning 1
Muscle Ruby on Rails Day 1 ~ Environment Construction ~
[Rails] ActiveRecord
[Rails] form_with
Rails Review 2