Ruby Review 1

Purpose of this post This is my own minutes of Ruby review. I will write a list of what I have forgotten from the knowledge I input before. Therefore, I just wrote it with my own satisfaction, so I plan to erase it when I hit it in my brain.

Teaching materials used for learning Udemy's ["Introduction to Ruby on Rails-Learn Ruby and Rails from the Basics and Publish Web Applications to the Net"](https://www.udemy.com/course/the-ultimate-ruby-on-rails- I used bootcamp /) as a teaching material.

The purpose of purchase is the following two. ① Creating a Rails application on Cloud9 ② Deploy


① Array and hash

○ Array

qiita.rb


a = [Variable 1,Variable 2,Variable 3]

・ Subscripts start from 0 ・ To retrieve variable 2, a [1]

○ Empty judgment

qiita.rb


a.empty

⇨ true if the judgment is empty

○ Search character judgment

qiita.rb


a.include?('Search character')

⇨ Returns true if the array contains search characters

○ Storage of continuous values in an array

qiita.rb


aa = (0..100).to_a

⇨ Store n or more specified information in an array

○ Operation to array

qiita.rb


Array name.pop
Array name.shift
Array name<<value
Array name.join (' ')
Array name.sort!.reverse!

From top to bottom ・ Erase the last value ・ Erase the first value ・ Add a value at the very end ・ Combine with one space for each subscript ・ Reverse the contents of the array

○ Hash

You can create an array of 2 variables by specifying the key.

qiita.rb


hash= {Key 1=>Value 1,Key 2=>Value 2,Key 3=>Value 3}

qiita.rb


puts hash['Key 1']
hash['Key 4'] =Value 4
hash['Key 1'] =Value 1
hash.delete('Key 1')     

From top to bottom ・ Extraction of value ・ Add value -Overwrite value -Delete value

○ Symbol

String on the source code Treated internally as an integer ⇨ Faster access inside the hash

qiita.rb


hash= {Key 1:Value 1,Key 2:Value 2,Key 3:Value 3}

qiita.rb


puts hash[:Key 1]   
hash[:Key 4] =Value 4
hash[:Key 1] =Value 1`     
hash.delete(:Key 1)      
hash.has_key?(:The key you want to find n)  
hash.size                                         

From top to bottom ・ Extraction of value ・ Add value -Overwrite value -Delete value ・ Do you have the key you want to find? Confirm ・ How many keys do you have? Confirmation

②each array and each hash

○ each array

qiita.rb


numbers = [1,2,3,4,5]
numbers.each do |number|
numbers = [1,2,3,4,5] numbers.each { |number| puts number}

○ each cache

qiita.rb


scores = {luke: 100,jack: 90,robert: 70}
scores.each do |k,v|
     puts v
     puts "#{k},#{v}"
end

③ Create class

qiita.rb


class Car
  def initialize(name)
    puts 'initialize'
    @name = name
  end
  
  def hello
    puts "Hello! I am #{@name}."
  end
end

boxter = Car.new('boxter')
boxter.hello

carrera = Car.new('carrera')
carrera.hello

[Explanation] -Variables when creating a class are capitalized for the first character -The argument specified when creating an instance can be specified as the return value of initialze. -Instance creation is like creating a clone when running a class. ⇨ Variable = class name.new () when declaring -Def initialize is the process executed when the instance is created. -The instance variable is the @ variable defined in initialize. ⇨ Can be used anywhere in Class

④ Instance method

qiita.rb


class Car

  def initialize(name)
    @name = name
  end
  
  def hello
    puts "Hello! I am #{@name}."
  end
end

def name
  @name
end

def name = (value)
  @name = value
end

boxter = Car.new('boxter')
boxter.hello
boxter.name
boxter.name = '718boxter'
puts boxter.name

[Explanation] ・ If there is no def name part, @name cannot be accessed from outside the class. (The instance variable (@name) cannot be accessed from outside the class) -Accessible by executing the .name method after creating the instance ・ The value @name corresponding to the .name method can be obtained. -Call these'instance methods' (getter methods)

・ About def nama = (value) ⇨ By setting boxter.name ='boxter' after creating the instance Any value can be specified for @name. It can also be output (Description method peculiar to ruby)

⑤ Accessor method

qiita.rb


def name
  @name
end

def name = (value)
  @name = value
end

All

qiita.rb


attr_accessor :name 

Can be achieved with

attr_accessor method ⇨ How developers don't have to write their own instance methods (Called property in other languages)

[When executing only reading]

qiita.rb


attr_reader :name 

def name part can be described in one line ⇨ You can just pick up @name

[When executing only writing]

qiita.rb


attr_writer :name 

def name = (value) part can be described in one line ⇨ You can only write to @name

⑥ Class variables

qiita.rb


class Car
  @@count
  def initialize(name)
    puts 'initialize'
    @name = name
    @@count += 1
  end

  def self.info
    puts "Test"
  end
  
  def hello
    puts "Hello! I am #{@name}."
  end
end

-Variables used in Class ・ The starting character starts with @@ 2 characters -Variables required when dealing with variables that you do not want to be reset for each def ⇨ Effective when you want to count the number of instantiations ⇨ Effective when you want to count the number of method executions

⑦ Class method Methods that can be called directly from the class

qiita.rb


class Car
  def initialize(name)
    puts 'initialize'
    @name = name
  end

  def self.info
    puts "Test"
  end
  
  def hello
    puts "Hello! I am #{@name}."
  end
end

Car.info
boxter = Car.new('boxter')
boxter.hello

carrera = Car.new('carrera')
carrera.hello

[Explanation] -Write the method as self. method name -Methods that can be used without instantiation, such as Car.info ・ If it is a normal method, it should not be usable unless it is boxter.info


Recommended Posts

Ruby Review 2
Ruby Review 1
Ruby cherry book review
Review of Ruby basic grammar
Ruby learning 4
Rails Review 1
[Ruby] Array
Ruby basics
Ruby learning 5
Ruby basics
Extraction of "ruby" double hash * Review
Ruby addition
Refactoring Ruby
Ruby learning 3
Java review
Encapsulation review
Ruby problem ⑦
Basics of Ruby ~ Review of confusing parts ~
Ruby learning 2
[Ruby] Block
ruby calculator
Ruby learning 6
[Ruby] Review about nesting of each
Ruby settings 1
Refactoring Ruby
Ruby basics
Ruby memo
Ruby learning 1
[Ruby] Module
Rails Review 2
With ruby ● × Game and Othello (basic review)
Introduction to Ruby 2
Ruby input / output
Review about bundler
ruby Uppercase letters
ruby search problem
Ruby Learning # 25 Comments
ruby constant variable
Ruby text conversion
Ruby basic terms
ruby exception handling
Ruby Learning # 13 Arrays
About Ruby symbols
Ruby Learning # 1 Introduction
[ruby] drill output
I started Ruby
[Ruby] Iterative processing
ruby API problem
Ruby vertical writing
About ruby ​​form
[ruby] drill output
About Ruby Hashes
Ruby setting 3 Rubocop
Ruby Learning # 14 Hashes
[ruby] drill output
[Ruby] each nested
[Ruby] Arithmetic progression
[Ruby] FizzBuzz problem
Ruby Hash notes
Class in Ruby
[Ruby] slice method