Home>
Count the number of method calls

I want to create a function that outputs the number of method calls, but I don't want to do it.
For example, if you execute the method three times, I want to display the following:

"1: Hello"
"2: Hello"
"3: Hello"

Error message

"1: Hello"
"1: Hello"
"1: Hello"
Will be output

Applicable source code
def hello
  count = 1
  puts "# {@ count}: Hello"
  count + = 1
end
hello
hello
hello
Tried

Don't just use instance variables.
What should I do?

  • Answer # 1

    module Hello
      @@ n = 0
      def hello
        n + = 1
        puts 'hello% s'% @@ n
      end
      module_function: hello
    end
    Hello.hello
    Hello.hello
    Hello.hello

  • Answer # 2

    Well, I understand somehow

      

    I don't want you to think.

    or

      

    Don't just use variables.

    Please statewhat and how bad, not

    .

    Because you listen to other people even as a hobby, you should explain it to the other party. Even more so if you work.

    Maybe delusion from code,

    Originally

    1: hello
    2: hello
    3: hello

    I want you to be

    1: hello
    1: hello
    1: hello

    Isn't it counting up with

    ?

    That's right.

    Reading variables and constants (Ruby 2.5.0)

    If you add

    "$" ;, it will be a global variable, otherwise it will be a local variable.

    Local variables are reserved at the beginning of the function and discarded at the end, for example, at the end (or out of it). (In case of stack area.)

    So

    [First time]:
    Start: count = 1
    Display section
    Before the end: count + 1 = 1 + 1 = 2
    Exit the block
    Discard

    [Second time]:
    Start: count = 1
    Display section
    Before: count + 1 = ...

    It feels like

    and this is repeated endlessly.
    So it will not be like 1,2,3,4 ...

    If you really do

    Method 1:
    Use global variables
    (Danger !!!!!! Deprecated.)
    Method 2:
    Managed by the caller and passed as an argument when calling the hello function
    Get with return value
    def hello (num)
      # Show here
      return num + 1
    end
    n = 1
    n = hello (n)
    n = hello (n)
    n = hello (n)

    Like
    Argument->Return value->Argument->Return value ...

    Method 3:
    Keep the class field equivalent to count and access it
    * Ruby seems to be called a class variable.


    Reference 1, Reference 2

  • Answer # 3

    I wrote three programs.
    Execution result

    hellow_1.rb

    $count = 0
    def hello
      $count + = 1
      puts "# {$count}: Hello"
    end
    hello
    hello
    hello


    hellow_2.rb

    class TraceX
      @@ count = Hash.new (0)
      def self.count (name)
        @@ count [name]
      end
      def self.count_up (name)
        @@ count [name] + = 1
      end
    end
    def hello
      TraceX.count_up (__ method__)
      puts "# {TraceX.count (__ method __)}: Hello"
    end
    hello
    hello
    hello


    hellow_3.rb

    $count = Hash.new (0)
    TracePoint.trace (: call) do | tp |
      $count [tp.method_id] + = 1
    end
    def hello
      puts "# {$count [__ method __]}: Hello"
    end
    hello
    hello
    hello

    hellow_2.rb does not use global variables.
    hellow_3.rb prevents you from forgetting to up the counter.
    hellow_2.rb and hellow_3.rb usemethodto make it easier to manage the number of calls to multiple methods.

    Hello_3.rb can be short, but global variables are used ...
    It might be good to create a class like hellow_2.rb, have counter information in the class variable, and count up with the TracePoint function.

    Reference information

    I examined Ruby's TracePoint
    https://qiita.com/siman/items/9426ff6c113247088f7e

    Use [Ruby] caller to log method calls
    https://qiita.com/katoy/items/a4dac843383185019f9d