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]
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?
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