assert('Kernel#send', '15.3.1.3.44') do # test with block l = send(:lambda) do true end assert_true l.call assert_equal l.class, Proc # test with argument assert_true send(:respond_to?, :nil?) # test without argument and without block assert_equal send(:to_s).class, String end assert('Kernel#instance_variable_defined?', '15.3.1.3.20') do o = Object.new o.instance_variable_set(:@a, 1) assert_true o.instance_variable_defined?("@a") assert_false o.instance_variable_defined?("@b") assert_true o.instance_variable_defined?("@a"[0,2]) assert_true o.instance_variable_defined?("@abc"[0,2]) assert_raise(NameError) { o.instance_variable_defined?("@0") } end assert('Kernel#instance_variable_get', '15.3.1.3.21') do o = Class.new { attr_accessor :foo, :bar }.new o.foo = "one" o.bar = 2 assert_equal("one", o.instance_variable_get(:@foo)) assert_equal(2, o.instance_variable_get("@bar")) assert_equal(nil, o.instance_variable_get(:@baz)) %w[foo @1].each do |n| assert_raise(NameError) { o.instance_variable_get(n) } end end assert('Kernel#instance_variable_set', '15.3.1.3.22') do o = Class.new { attr_reader :foo, :_bar }.new assert_equal("one", o.instance_variable_set(:@foo, "one")) assert_equal("one", o.foo) assert_equal(2, o.instance_variable_set("@_bar", 2)) assert_equal(2, o._bar) %w[@6 @% @@a @ a].each do |n| assert_raise(NameError) { o.instance_variable_set(n, 1) } end assert_raise(FrozenError) { o.freeze.instance_variable_set(:@a, 2) } assert_raise(FrozenError, ArgumentError) { nil.instance_variable_set(:@a, 2) } end assert('Kernel#instance_variables', '15.3.1.3.23') do o = Object.new o.instance_eval do @a = 11 @b = 12 end ivars = o.instance_variables assert_equal Array, ivars.class assert_equal(2, ivars.size) assert_true ivars.include?(:@a) assert_true ivars.include?(:@b) end assert('Kernel#methods', '15.3.1.3.31') do assert_equal Array, methods.class assert_equal [:foo], Class.new{def self.foo; end}.methods(false) assert_equal [], Class.new{}.methods(false) end assert('Kernel#private_methods', '15.3.1.3.36') do assert_equal Array, private_methods.class end assert('Kernel#protected_methods', '15.3.1.3.37') do assert_equal Array, protected_methods.class end assert('Kernel#public_methods', '15.3.1.3.38') do assert_equal Array, public_methods.class class Foo def foo end end assert_equal [:foo], Foo.new.public_methods(false) end assert('Kernel#singleton_methods', '15.3.1.3.45') do assert_equal singleton_methods.class, Array end assert('Kernel.global_variables', '15.3.1.2.4') do assert_equal Array, Kernel.global_variables.class end assert('Kernel#global_variables', '15.3.1.3.14') do variables1 = global_variables assert_equal Array, variables1.class assert_not_include(variables1, :$kernel_global_variables_test) $kernel_global_variables_test = nil variables2 = global_variables assert_include(variables2, :$kernel_global_variables_test) assert_equal(1, variables2.size - variables1.size) end assert('Kernel#local_variables', '15.3.1.3.28') do assert_equal Array, local_variables.class def local_var_list a = "hello" local_variables end assert_equal [:a], local_var_list end assert('Kernel.local_variables', '15.3.1.2.7') do a, b = 0, 1 a += b vars = Kernel.local_variables.sort assert_equal [:a, :b, :vars], vars assert_equal [:a, :b, :c, :vars], Proc.new { |a, b| c = 2 # Kernel#local_variables: 15.3.1.3.28 local_variables.sort }.call(-1, -2) a = Object.new def a.hoge(vars, *, **) Proc.new { x, y = 1, 2 local_variables.sort } end assert_equal([:vars, :x, :y]) { a.hoge(0).call } end assert('Kernel#define_singleton_method') do o = Object.new ret = o.define_singleton_method(:test_method) do :singleton_method_ok end assert_equal :test_method, ret assert_equal :singleton_method_ok, o.test_method assert_raise(TypeError) { 2.define_singleton_method(:f){} } assert_raise(FrozenError) { [].freeze.define_singleton_method(:f){} } end assert('Kernel#singleton_class') do o1 = Object.new assert_same(o1.singleton_class, class << o1; self end) o2 = Object.new sc2 = class << o2; self end assert_same(o2.singleton_class, sc2) o3 = Object.new sc3 = o3.singleton_class o3.freeze assert_predicate(sc3, :frozen?) assert_predicate(Object.new.freeze.singleton_class, :frozen?) end def labeled_module(name, &block) Module.new do (class <