How do I call a method that is a hash value?

前端 未结 7 770
后悔当初
后悔当初 2021-01-07 02:47

Previously, I asked about a clever way to execute a method on a given condition \"Ruby a clever way to execute a function on a condition.\"

The solutions and respons

7条回答
  •  甜味超标
    2021-01-07 03:12

    I am pretty new to using callbacks in Ruby and this is how I explained it to myself using an example:

    require 'logger'
    log = Logger.new('/var/tmp/log.out')
    
    def callit(severity, msg, myproc)
      myproc.call(sev, msg)
    end
    
    lookup_severity = {}
    lookup_severity['info'] = Proc.new { |x| log.info(x) }
    lookup_severity['debug'] = Proc.new { |x| log.debug(x) }
    
    logit = Proc.new { |x,y| lookup_sev[x].call(y) }
    
    callit('info', "check4", logit)
    callit('debug', "check5", logit)
    

提交回复
热议问题