What kind of method is define_method?

What kind of method is define_method?

When I was reading gem, I found a method called define_method. I wasn't sure, so I looked it up. Translated literally, it defines a method. The method named. I don't understand more and more. ..

So let's look at an example.

ruby.rb


NUMBERS = %w(zero one two three four five six seven eight nine)

NUMBERS.each_with_index do |word, num|
  define_method word do |i = nil|
    i ? num * i : num
  end
end

p two      #=> 2
p two(2)   #=> 4
p nine     #=> 9
p nine(3)  #=> 27

** define_method is a method that can dynamically define method ** Even in the above example, the methods such as two and nine are not directly defined. It can be used as a method. This is because define_method dynamically defines two, nine methods.

ruby.rb


NUMBERS.each_with_index do |word, num|
  define_method word do |i = nil|
    i ? num * i : num
  end
end

word is passed as an argument of define_method. This is the dynamically defined method name. Word contains the elements of the array ["zero", "one", "two", ... "nine"]. So I could use the two, nine methods.

And the block part of define_method is the processing content of the dynamically defined method.

ruby.rb


define_method word do |i = nil|
    i ? num * i : num
  end

As the content, if i exists, i * num is returned and If it doesn't exist, it returns num.

And i is the argument of the newly defined method. Finally, let's look at the processing result again.

p two      #=> 2 #Returns num because there are no arguments
p two(2)   #=> 4 #2 because there are arguments* 2 = 4
p nine     #=> 9 #Returns num because there are no arguments
p nine(3)  #=> 27 #9 because there are arguments* 3 = 27

This is the end of the explanation of define_method. I hope you find it useful.

** 86 days to become a full-fledged engineer **

Recommended Posts

What kind of method is define_method?
What kind of StringUtil is good
What is the pluck method?
What is the initialize method?
'% 02d' What is the percentage of% 2?
What is it? ~ 3 types of "no" ~
Road to Java Engineer Part2 What kind of language is Java?
What is testing? ・ About the importance of testing
[For programming beginners] What is a method?
What is the main method in Java?
What is the data structure of ActionText?
What is Cubby
What is Docker?
What is null? ]
What is java
What is Keycloak
What is maven?
What is Jackson?
What is Docker
What is self
What is Jenkins
What is IM-Juggling?
What is params
What is Facade? ??
What is Java <>?
What is Gradle?
What is POJO
What is Java
What is centOS
What is RubyGem?
What is programming?
What is before_action?
What is Docker
What is Byte?
What is Tomcat
What is JSP? ~ Let's know the basics of JSP !! ~
What is Maven Assembly?
Existence check of has_many and belongs_to-optional: What is true?
What is `docker-compose up`?
What is vue cli
Understanding ruby's "|| =" specification and what is Rails presence method?
What is an interface?
What is Ruby's self?
What is Ruby's attr_accessor?
What is Java Encapsulation?
What is permission denied?
What is instance control?
What is an initializer?
What is Spring Tools 4
What is an operator?
What is Guava's @VisibleForTesting?
definition of ruby method
What is MVC model?
What is an annotation?
What is Java technology?
What is Java API-java
What is Gradle's Artifact?
What is JPA Auditing?
[Swift] What is dismiss?
[Java] What is flatMap?
What is a Servlet?