[RUBY] Invoke the character string passed as an argument as a method with send

Overview

I learned how to use the send method, so make a note.

I had a form where I could attach up to two images, and I wanted to find out how big the post was, so I was trying to put in a log.

So I originally wrote the following code.

Before refactoring

validate :checking_image_size_over_limit

def checking_image_size_over_limit
  logging_image_size(image_first, 'first') if image_first.present?
  logging_image_size(image_second, 'second') if image_second.present?
end

def logging_image_size(image, number)
  if image&.size >= 5 * 1024 * 1024
    Rails.logger.info "[#{self.class.name}] Over 5MB of images (image_#{number})"
  end
end

I wanted to determine whether it was the first or the second, so I passed a string as the second argument and expanded it in the log message. However, I received a review from my senior and rewrote it, saying, "If so, you can use send with just one argument."

After refactoring

validate :checking_image_size_over_limit

def checking_image_size_over_limit
  logging_image_size('image_first') if image_first.present?
  logging_image_size('image_second') if image_second.present?
end

def logging_image_size(image)
  if send(image)&.size >= 5 * 1024 * 1024
    Rails.logger.info "[#{self.class.name}] Over 5MB of images (#{image})"
  end
end

Speaking of send, I only had the image of using it to dynamically switch methods, but I learned that it can also be used by invoking the character string passed as an argument as a method again.

Recommended Posts

Invoke the character string passed as an argument as a method with send
Read the file under the classpath as a character string with spring
[Ruby] I want to make an array from a character string with the split method. And vice versa.
About the method to convert a character string to an integer / decimal number (cast data) in Java
Come out with a suffix on the method
Come out with a suffix on the method 2
Arguments with default values Take the full_title method of the Rails tutorial as an example
[Java] Cut out a part of the character string with Matcher and regular expression
[Ruby] Cut out a string using the slice method
Send an email with a PDF attachment via JavaMail
Note No. 6 "Calculate the formula for the one-digit sum difference received as a character string" [Java]
Get an image as a character string (resource name) and paste it into ImageView (kotiln)
Set the date and time from the character string with POI
Precautions when using Mockito.anyString as an argument when Mocking with Mockito
Java learning_Behavior when there is a method with the same name as a field with the same name in two classes that have an inheritance relationship
[Java] How to search for a value in an array (or list) with the contains method
Code to specify the image name as a character string and paste it into ImageView (android)
[ruby] Method call with argument
The basics of the process of making a call with an Android app
When the character string passed to C ++ by JNA is garbled
[Ruby] Difference between symbol variables and character string variables. About the difference between [: a] and ['a'].
Sorting a list with an int array as an element (Java) (Comparator)
Connecting to a database with Java (Part 1) Maybe the basic method
[Rails] How to omit the display of the character string of the link_to method