ruby 装饰Ruby类中的每个方法

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby 装饰Ruby类中的每个方法相关的知识,希望对你有一定的参考价值。

# make class convertable to JSON format (unused in this snippet)
class JSONable
    def to_json
        hash = {}
        self.instance_variables.each do |var|
            hash[var] = self.instance_variable_get var
        end
        hash.to_json
    end
    def from_json! string
        JSON.load(string).each do |var, val|
            self.instance_variable_set var, val
        end
    end
end

class Object
    def self.inherited(klass)
        def klass.method_added(name)
            return if @_not_new
            @_not_new = true
            original = "original #{name}"
            alias_method original, name
            puts "#{name}"
            define_method(name) do |*args, &block|
                puts "==> called  #{name} with args: #{args.inspect}"
                result = send original, *args, &block
                puts "<== result is #{result}"
                result
            end
            @_not_new = false
        end
    end
    
    # serialize object to JSON format
    define_method :tojson do
        # get all instance variables
        ivar = instance_variables.each_with_object({}) { |var, hash| hash[var.to_s.delete("@")] = instance_variable_get(var) }
        # get all class variables (static var)
        cvar = self.class.class_variables.each_with_object({}) { |var, hash| hash[var.to_s.delete("@")] = self.class.class_variable_get(var) }
        # merge it into one hash
        ivar.merge!(cvar)
    end
end

class Product #  < JSONable
    @@ss = 100 # static
    def self.sides
        @ss
    end
    
    def meth(a1, a2)
        @value = 10
        @string = "10"
        a1 + a2
    end   
end


product = Product.new
puts product.meth(2,3)

# hash = product.instance_variables.each_with_object({}) { |var, hash| hash[var.to_s.delete("@")] = product.instance_variable_get(var) }
# p hash # => {"name"=>"book", "price"=>15.95}

product.tojson

puts "OR DO it another way"

require 'oj'

class Foo
    def initialize()
        @aMemberVar = 'aMemberVar Member Variable' 
        @aFuncName = 'aMemberFunc' 
        @__pr = 'private'
    end
end

foo = Foo.new

puts Oj.dump foo
# product.class.instance_variables
# puts Product.class_variables
module MethodLogger
  def self.included(base)
    methods = base.instance_methods(false) + base.private_instance_methods(false)
    base.class_eval do
      methods.each do |method_name|
        original_method = instance_method(method_name)
        define_method(method_name) do |*args, &block|
          # Rails.logger.info "-> #{base}##{method_name}(#{args.inspect})"
          puts "-> #{base}##{method_name}(#{args.inspect})"
          return_value = original_method.bind(self).call(*args, &block)
          puts "<- #{base}##{method_name} #=> #{return_value.inspect}"
          return_value
        end
      end
    end
  end
end

class Model

  def foo
    "bar"
  end

  include MethodLogger
end


Model.new.foo

以上是关于ruby 装饰Ruby类中的每个方法的主要内容,如果未能解决你的问题,请参考以下文章

如何在Ruby中的Array类中对数组的每个元素进行平方?

Ruby 面向对象

雷林鹏分享:Ruby 面向对象

用于打印类中所有方法调用和返回的 Ruby 模块

Ruby - 在类中获取非祖先方法的数组

晕菜了,Ruby类中的变量!