[Rails] Method summary for conversion / verification / search

Conversion system method

to_h A method to convert to a hash.

to_a A method to convert to an array.

to_s A method to convert to a string.

to_i A method to convert to a number.

to_sym A method that returns a symbol.

encode Convert the character code of the character string.

String.encode('UTF-8')

gsub You can replace a specific character with another character, or use a regular expression to replace or delete the relevant part.

String.gsub(置換したいString, 置換後のString)

String.gsub(/Regular expressions/, Regular expressionsに該当した箇所を置換した後のString)

compact Creates and returns a new array with the nil elements removed.


a = [1, nil, 'abc', false]
b = a.compact
b #=> [1, 'abc', false]

join A method that can separate array elements by a specified one.


p ["apple", "orange", "lemon"].join(',') #=> "apple,orange,lemon"

each_slice It is used when you want to divide by a certain number of elements.


[1..10].each_slice(2) do |num|
  puts num
end
#=> [1,2,3,4,5],[6,7,8,9,10]

Verification method

request.post(patch,get,delete)? It verifies whether the request type is post (patch, get, delete).

valid? As a result of executing validation, true is returned if there is no error, and false if there is no error.

invalid? As a result of executing validation, false is returned if there is no error, and true is returned if there is no error.

present? Returns true if there is a value, false otherwise.

nil? Returns true only for nil, false otherwise.

empty? Returns true if it is an empty string or array, false otherwise. (If you use it for nil, an error will occur.)

blank? Returns true if nil or empty, false otherwise.

zero? A method to check if the contents are 0 (whether or not). It can be used for anything other than strings and true & false.

any? A method that returns true when the contents are present. High-speed processing because only one case is searched. The second and third are synonymous.


Sample.where(name: "Samurai 1").any?
#=> true

Sample.any? do | sample |
  sample.name == "Samurai 1"
end
#=> true

Sample.any? { | sample | sample.name == "Samurai 1" }
#=> true

yes? Returns true if the user says yes.


freeze! if yes?("Should I freeze the latest Rails?")

list << 'Saturday' if holiday_saturday&.yes?

Search method

pluck Returns an array of specified columns from the table as a data type.


Product.pluck(:id)

map A method that executes processing for each element in order.


Array variables.map {|Variable name|Specific processing}

array = ["a", "b", "c"]
array = array.map {|item|  item.upcase } #=>["A", "B", "C"]

detect Evaluate each element in blocks and return the first one of the elements that is "true". Something like a find method.


array = [1, 2, 3, 1, 2, 3]
detect = array.detect {|v| v==1 } #=>1

Recommended Posts

[Rails] Method summary for conversion / verification / search
Summary of rails validation (for myself)
rails tutorial chapter 10 summary (for self-learning)
rails method
Binary search binary search method
Method to search
Rails delegate method
Binary search method
Rails 6.0 Routing Summary
rails db: 〇〇 Summary
Method summary to update multiple columns [Ruby on Rails]
Implement post search function in Rails application (where method)
[For Rails beginners] Implemented multiple search function without Gem
[Ruby on Rails] Search function (model, method selection formula)
Summary of file reading method for each Java file format
Rails hashtag search implementation
[Rails] devise-related routing summary
[Rails] Migration command summary
abbreviation for ruby method
[Rails] rails db command summary
Rails search function implementation
[Rails] devise introduction method
[Ruby] Method definition summary
[Rails] I tried using the button_to method for the first time
Ruby on Rails for beginners! !! Summary of new posting functions
Ruby on Rails record search, create if not find_or_create_by method
[Rails] Search method when date and time have each column
[Rails] Implemented a pull-down search function for Active Hash data