・ Relationship between class and instance The class is a parent and the instances are children, and there are many instances in the class. 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.
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
If you want to use instance variables such as @name properly, you have to do two things:
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.
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.
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)
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.
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)
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.
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.
number ||=10
If there is a number, number is used, otherwise number is used by substituting 10 for number.
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.
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.
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.
1.require: Used when reading the source code (user.rb in this case)
> require './user.rb'
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