从红宝石方法进来 [英] getting in-out from ruby methods

查看:117
本文介绍了从红宝石方法进来的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jruby来运行一堆ruby脚本,尽管我使用纯红宝石的一部分。



有时候从输出中难以跟踪到底究竟发生了什么,哪里发生错误。



我想在每个方法中得到类似这样的东西:
输入方法A
从方法A



现在我可以肯定地把这些意见放在每一种方法中,这种感觉很错误。有没有办法在一些更详细的地方运行ruby来获取我的日志中的这些信息。希望我会避免使用很多宝石等。由于这些都是在一些受管服务器上,我将不得不花一些时间来获得更多的s / f。希望某些东西可以作为jruby本身的一部分



谢谢!

解决方案

p>您可以使用以下代码:

  module InOutHook 
module ClassMethods
def setup_hooks(* syms)
syms.each do | sym | #对于每个符号
str_id =__#{sym} __ hooked__
除非private_instance_methods.include?(str_id)
alias_method str_id,sym#备份原始方法
private str_id#Make backup private
define_method sym do | * args | #替换方法
puts>>>#{self.class} \ ## {sym}>>>
ret = __send__ str_id,* args#Invoke backup
puts<#{self.class} \ ## {sym}>>>
ret
end
end
end
end
end
def InOutHook.included(base)
base.extend(ClassMethods )
end
end

class TestClass
def test1
putstest!
end
def test2(v)
putsValue is#{v}
end
include InOutHook
setup_hooks(:test1,:test2)
end

#对现有的课程也有:
class Array
include InOutHook
setup_hooks(:[])
end

tc = TestClass.new
tc.test1
tc.test2(10)

ary = [1,2,3]
puts ary [1..2]

如果你想添加一个hoot到每个方法,只需添加一个splat星号:

  setup_hooks(* []。methods)


I am using jruby to run bunch of ruby scripts, though I am using pure ruby part of it.

It sometimes gets difficult to follow from output what exactly is happening or where exactly something went wrong.

I wanted to get something like this in my std out for every method: entered in method A out of method A

Now I can surely go and put those comments in every method ..which feels very wrong. Is there a way to run ruby in a little verbose more to get this information in my log. Hopefully I would avoid using a lot of gems etc .. since these are on some managed servers and I will have to spend some time to just get more s/f on it. Hoping something would be avail as part of jruby itself

Thanks!

解决方案

You could use this code:

module InOutHook
  module ClassMethods
    def setup_hooks(*syms)
      syms.each do |sym| # For each symbol
        str_id = "__#{sym}__hooked__"
        unless private_instance_methods.include?(str_id)
          alias_method str_id, sym        # Backup original method
          private str_id                  # Make backup private
          define_method sym do |*args|    # Replace method
            puts ">>> #{self.class}\##{sym} >>>"
            ret = __send__ str_id, *args  # Invoke backup
            puts "<<< #{self.class}\##{sym} >>>"
            ret
          end
        end
      end
    end
  end
  def InOutHook.included(base)
    base.extend(ClassMethods)
  end
end

class TestClass
  def test1
    puts "test!"
  end
  def test2(v)
    puts "Value is #{v}"
  end
  include InOutHook
  setup_hooks(:test1, :test2)
end

# works on existing classes too:
class Array
  include InOutHook
  setup_hooks(:[])
end

tc = TestClass.new
tc.test1
tc.test2(10)

ary = [1,2,3]
puts ary[1..2]

In case you want to add a hoot to every method, just add a splat asterisk:

setup_hooks(*[].methods)

这篇关于从红宝石方法进来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆