ruby SimpleSpy是一种在开发时检查对象的简便方法。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby SimpleSpy是一种在开发时检查对象的简便方法。相关的知识,希望对你有一定的参考价值。

# Simple dev util
# Reveals the filename, line number, object type and value.
module SimpleSpy
  def spy(msg, len = 35)
    print '-' * len + "\n" + caller[0] + "\n" + msg.class.to_s
    print ' : ' + msg.to_s + "\n" + '-' * len + "\n" * 2
  end
end

include SimpleSpy

# Examples
spy val = Object.new
spy val = { 'key' => [{ 'key2' => 'Foo' }, { 'key2' => 'Bar' }] }
spy val = nil
spy val = true

def this_method
  val = 'FooBar'
end

spy this_method

# In a class
class SomeClass
  def initialize
    @val = method_in_class
  end

  def method_in_class
    spy val = some_value
  end

  def some_value
    10_000_000
  end
end

spy val = SomeClass.new

# -----------------------------------
# properties.rb:13:in `<main>'
# Object : #<Object:0x007fb5f28707d0>
# -----------------------------------
#
# -----------------------------------
# properties.rb:14:in `<main>'
# Hash : {"key"=>[{"key2"=>"Foo"}, {"key2"=>"Bar"}]}
# -----------------------------------
#
# -----------------------------------
# properties.rb:15:in `<main>'
# NilClass :
# -----------------------------------
#
# -----------------------------------
# properties.rb:16:in `<main>'
# TrueClass : true
# -----------------------------------
#
# -----------------------------------
# properties.rb:22:in `<main>'
# String : FooBar
# -----------------------------------
#
# -----------------------------------
# properties.rb:31:in `method_in_class'
# Fixnum : 10000000
# -----------------------------------
#
# -----------------------------------
# properties.rb:39:in `<main>'
# SomeClass : #<SomeClass:0x007fb5f286a5d8>
# -----------------------------------

以上是关于ruby SimpleSpy是一种在开发时检查对象的简便方法。的主要内容,如果未能解决你的问题,请参考以下文章

面向对象-1

Ruby 是按值传递还是按引用传递? [复制]

如何使用Rust提高Ruby性能?

什么是ruby?

返回 Ruby 正则表达式的第一个匹配项

一种在Java中跨ClassLoader的方法调用的实现