测试在kernel中定义的方法的作用对象

# =============================
# 目标1:
# 1、Kernel模块内定义实例方法,哪些对象可以使用
# 2、Kernel的单件方法哪些对象可以使用
# =============================
# 结论
# 1、Kernel模块内定义实例方法,ruby的所有对象都可以使用,因为Kernel会被Mixin入Object,其实例方法(用def定义的),可以
#    被所有ruby对象使用(MyClass/"string")
# 2、Kernel的单件方法只能被其自己使用,其它类和方法都不能使用(MyClass/MyClass.new)
# =============================
lambda {
  module Kernel
    def self.im_singleton_method
      "i'm a singleton method of Kernel define in Kernel"
    end

    def im_instance_method(from="Kernel")
      "i'm a instance method define in Kernel and call from #{from}"
    end
  end

  class MyClass; end
  puts Kernel.im_singleton_method
  puts "Kernel.singleton_methods:" + Kernel.singleton_methods(false).grep(/im_singleton_method/).to_s
  puts "Kernel.im_instance_method:" + Kernel.instance_methods(false).grep(/im_instance_method/).to_s
  puts MyClass.new.im_instance_method("obj")
  puts MyClass.im_instance_method("MyClass")
  puts begin MyClass.im_singleton_method rescue "undefined method `im_singleton_method' for MyClass:Class" end
  puts begin MyClass.new.im_singleton_method rescue "undefined method `im_singleton_method' for #<MyClass:0x007fbb22813640>" end

  # ================test1 OUTPUT================
  # i'm a singleton method of Kernel define in Kernel
  # Kernel.singleton_methods:[:im_singleton_method]
  # Kernel.im_instance_method:[:im_instance_method]
  # i'm a instance method define in Kernel and call from obj
  # i'm a instance method define in Kernel and call from MyClass
  # undefined method `im_singleton_method' for MyClass:Class
  # undefined method `im_singleton_method' for #<MyClass:0x007fbb22813640>
  # ================test1 OUTPUT================
}.call