mrubyedge 2.1.2

mruby/edge is yet another mruby that is specialized for running on WASM
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
##
# Kernel ISO Test

assert('Kernel', '15.3.1') do
  assert_equal Module, Kernel.class
end

# Kernel.block_given? is not provided by mruby. '15.3.1.2.2'

# Kernel.eval is provided by the mruby-eval mrbgem. '15.3.1.2.3'

# Kernel.iterator? is not provided by mruby. '15.3.1.2.5'

# Kernel.lambda is not provided by mruby. '15.3.1.2.6'

# Kernel.loop is not provided by mruby. '15.3.1.2.8'

# Kernel.p test is skipped due to the side effect. '15.3.1.2.9'
#assert('Kernel.p', '15.3.1.2.9') do
#  assert_equal 1, Kernel.__send__(:p, 1)
#end

# Kernel.print is provided by the mruby-io mrbgem. '15.3.1.2.10'

# Kernel.puts is provided by the mruby-io mrbgem. '15.3.1.2.11'

assert('Kernel.raise', '15.3.1.2.12') do
  assert_raise RuntimeError do
    Kernel.raise
  end

  assert_raise RuntimeError do
    Kernel.raise RuntimeError.new
  end
end

assert('Kernel#__id__', '15.3.1.3.3') do
  assert_equal Integer, __id__.class
end

assert('Kernel#__send__', '15.3.1.3.4') do
  # test with block
  l = __send__(:lambda) do
    true
  end

  assert_true l.call
  assert_equal Proc, l.class
  # test with argument
  assert_true __send__(:respond_to?, :nil?)
  # test without argument and without block
  assert_equal String, __send__(:to_s).class

  args = [:respond_to?, :nil?]
  assert_true __send__(*args)
  assert_equal [:respond_to?, :nil?], args
end

assert('Kernel#block_given?', '15.3.1.3.6') do
  def bg_try(&b)
    if block_given?
      yield
    else
      "no block"
    end
  end

  assert_false block_given?
  assert_equal "no block", bg_try
  assert_equal "block" do
    bg_try { "block" }
  end
  assert_equal "block" do
    bg_try do
      "block"
    end
  end

  def bg_try_in_block
    -> { block_given? }[]
  end

  assert_false bg_try_in_block
  assert_true bg_try_in_block{}
end

assert('Kernel#class', '15.3.1.3.7') do
  assert_equal Module, Kernel.class
end

assert('Kernel#clone', '15.3.1.3.8') do
  class KernelCloneTest
    def initialize
      @v = 0
    end

    def get
      @v
    end

    def set(v)
      @v = v
    end
  end

  a = KernelCloneTest.new
  a.set(1)
  b = a.clone

  def a.test
  end
  a.set(2)
  c = a.clone

  immutables = [ 1, :foo, true, false, nil ]
  error_count = 0
  immutables.each do |i|
    begin
      i.clone
    rescue TypeError
      error_count += 1
    end
  end

  assert_equal 2, a.get
  assert_equal 1, b.get
  assert_equal 2, c.get
  assert_true a.respond_to?(:test)
  assert_false b.respond_to?(:test)
  assert_true c.respond_to?(:test)

  a.freeze
  d = a.clone
  assert_true d.frozen?
end

assert('Kernel#dup', '15.3.1.3.9') do
  class KernelDupTest
    def initialize
      @v = 0
    end

    def get
      @v
    end

    def set(v)
      @v = v
    end
  end

  a = KernelDupTest.new
  a.set(1)
  b = a.dup

  def a.test
  end
  a.set(2)
  c = a.dup

  assert_equal 2, a.get
  assert_equal 1, b.get
  assert_equal 2, c.get
  assert_true a.respond_to?(:test)
  assert_false b.respond_to?(:test)
  assert_false c.respond_to?(:test)
end

assert('Kernel#dup class') do
  assert_nothing_raised do
    Array.dup.new(200)
    Range.dup.new(2, 3)
    String.dup.new("a"*50)
  end
end

# Kernel#eval is provided by mruby-eval mrbgem '15.3.1.3.12'

assert('Kernel#extend', '15.3.1.3.13') do
  class Test4ExtendClass
  end

  module Test4ExtendModule
    def test_method; end
  end

  a = Test4ExtendClass.new
  a.extend(Test4ExtendModule)
  b = Test4ExtendClass.new

  assert_true a.respond_to?(:test_method)
  assert_false b.respond_to?(:test_method)

  assert_raise(FrozenError) { Object.new.freeze.extend(Test4ExtendModule) }
  assert_raise(FrozenError, TypeError) { :sym.extend(Test4ExtendModule) }
end

assert('Kernel#extend works on toplevel', '15.3.1.3.13') do
  module Test4ExtendModule
    def test_method; end
  end
  # This would crash...
  extend(Test4ExtendModule)

  assert_true respond_to?(:test_method)
end

assert('Kernel#freeze') do
  obj = Object.new
  assert_equal obj, obj.freeze
  assert_equal 0, 0.freeze
  assert_equal :a, :a.freeze
  assert_equal true, true.freeze
  assert_equal false, false.freeze
  assert_equal nil, nil.freeze
  skip unless Object.const_defined?(:Float)
  assert_equal 0.0, 0.0.freeze
end

assert('Kernel#frozen?') do
  assert_false "".frozen?
  assert_true "".freeze.frozen?
  assert_true 0.frozen?
  assert_true :a.frozen?
  assert_true true.frozen?
  assert_true false.frozen?
  assert_true nil.frozen?
  skip unless Object.const_defined?(:Float)
  assert_true 0.0.frozen?
end

assert('Kernel#hash', '15.3.1.3.15') do
  assert_equal hash, hash
end

assert('Kernel#inspect', '15.3.1.3.17') do
  s = inspect

  assert_equal String, s.class
  assert_equal "main", s
end

assert('Kernel#inspect leaves out an ivar Ruby cannot name') do
  # C extensions keep private state in ivars whose names have no '@'
  # (mrb_iv_set with a bare symbol); instance_variables already leaves them
  # out, and inspect must not print them either.
  o = Object.new
  o.__iv_set_hidden(:secret, "hidden")
  assert_not_include o.inspect, "secret"
  assert_equal o.to_s, o.inspect

  o.__iv_set_hidden(:@pub, 1)
  assert_include o.inspect, "@pub=1"
  assert_not_include o.inspect, "secret"
end

assert('Kernel#inspect survives a callback that grows the object mid-walk (GHSA-j6fq-xj4w-877x)') do
  # inspect calls each ivar value's own #inspect, which can run arbitrary
  # Ruby. Adding a new ivar to the object being inspected from in there
  # used to free its shaped storage (shaped_iv_set growing to a wider
  # block) out from under the walk still reading the old one.
  owner_class = Class.new do
    def initialize(mutator_class)
      @first = mutator_class.new(self)
      @second = "kept"
    end

    def grow
      @third = :grown
    end
  end
  mutator_class = Class.new do
    def initialize(owner)
      @owner = owner
    end

    def inspect
      @owner.grow
      "mutated"
    end
  end

  o = owner_class.new(mutator_class)
  s = o.inspect
  assert_include s, "@first=mutated"
  assert_include s, '@second="kept"'
end

assert('Kernel#inspect survives a callback that removes an ivar mid-walk (GHSA-j6fq-xj4w-877x)') do
  # remove_instance_variable de-shapes the object (shaped storage to a
  # plain table), freeing the same shaped block a growing callback frees;
  # the walk used to keep reading through it for the remaining keys.
  owner_class = Class.new do
    def initialize(remover_class)
      @a = remover_class.new(self)
      @b = "kept"
      @c = "also kept"
    end

    def drop_b
      remove_instance_variable(:@b)
    end
  end
  remover_class = Class.new do
    def initialize(owner)
      @owner = owner
    end

    def inspect
      @owner.drop_b
      "dropped"
    end
  end

  o = owner_class.new(remover_class)
  s = o.inspect
  assert_include s, "@a=dropped"
  assert_include s, '@c="also kept"'
end

assert('Kernel#is_a?', '15.3.1.3.24') do
  assert_true is_a?(Kernel)
  assert_false is_a?(Array)

  assert_raise TypeError do
    42.is_a?(42)
  end
end

assert('Kernel#iterator?', '15.3.1.3.25') do
  assert_false iterator?
end

assert('Kernel#kind_of?', '15.3.1.3.26') do
  assert_true kind_of?(Kernel)
  assert_false kind_of?(Array)
end

assert('Kernel#lambda', '15.3.1.3.27') do
  l = lambda do
    true
  end

  m = lambda(&l)

  assert_true l.call
  assert_equal Proc, l.class
  assert_true m.call
  assert_equal Proc, m.class
end

assert('Kernel#loop', '15.3.1.3.29') do
  i = 0

  loop do
    i += 1
    break if i == 100
  end

  assert_equal i, 100
end

assert('Kernel#method_missing', '15.3.1.3.30') do
  class MMTestClass
    def method_missing(sym)
      "A call to #{sym}"
    end
  end
  mm_test = MMTestClass.new
  assert_equal 'A call to no_method_named_this', mm_test.no_method_named_this

  class SuperMMTestClass < MMTestClass
    def no_super_method_named_this
      super
    end
  end
  super_mm_test = SuperMMTestClass.new
  assert_equal 'A call to no_super_method_named_this', super_mm_test.no_super_method_named_this

  class NoSuperMethodTestClass
    def no_super_method_named_this
      super
    end
  end
  no_super_test = NoSuperMethodTestClass.new
  msg = "no superclass method 'no_super_method_named_this' for NoSuperMethodTestClass"
  assert_raise_with_message(NoMethodError, msg) do
    no_super_test.no_super_method_named_this
  end

  a = String.new
  msg = "undefined method 'no_method_named_this' for String"
  assert_raise_with_message(NoMethodError, msg) do
    a.no_method_named_this
  end

  a = Object.new
  def a.method_missing(mid, *args, **opts, &blk)
    [mid, args, opts, blk&.call]
  end
  assert_equal [:foo, [], {}, nil], a.foo
  assert_equal [:foo, [1, 2], {}, nil], a.foo(1, 2)
  assert_equal [:foo, (1..15).to_a, {}, nil], a.foo(*(1..15).to_a)
  assert_equal [:foo, [], { k: 1 }, nil], a.foo(k: 1)
  assert_equal [:foo, [1], { k: 2 }, nil], a.foo(1, k: 2)
  assert_equal [:foo, [1, 2], {}, 3], a.foo(1, 2) { 3 }
  assert_equal [:foo, [1], { k: 2 }, 3], a.foo(1, k: 2) { 3 }
  assert_equal [:foo, [], { k: 1 }, 2], a.foo(k: 1) { 2 }
end

assert('Kernel#nil?', '15.3.1.3.32') do
  assert_false nil?
end

assert('Kernel#object_id', '15.3.1.3.33') do
  a = ""
  b = ""
  assert_not_equal a.object_id, b.object_id

  assert_kind_of Numeric, object_id
  assert_kind_of Numeric, "".object_id
  assert_kind_of Numeric, true.object_id
  assert_kind_of Numeric, false.object_id
  assert_kind_of Numeric, nil.object_id
  assert_kind_of Numeric, :no.object_id
  assert_kind_of Numeric, 1.object_id
  assert_kind_of Numeric, 1.0.object_id
end

# Kernel#p test is skipped due to the side effect. '15.3.1.3.34'
#assert('Kernel#p', '15.3.1.3.34') do
#  assert_equal nil, p
#  assert_equal nil, p(p)
#  assert_equal [:a, :b], p(:a, :b)
#end

# Kernel#print is defined in mruby-io mrbgem. '15.3.1.3.35'

# Kernel#puts is defined in mruby-io mrbgem. '15.3.1.3.39'

assert('Kernel#raise', '15.3.1.3.40') do
  assert_raise RuntimeError do
    raise
  end

  assert_raise RuntimeError do
    raise RuntimeError.new
  end
end

assert('Kernel#remove_instance_variable', '15.3.1.3.41') do
  class Test4RemoveInstanceVar
    attr_reader :var
    def initialize
      @var = 99
    end
    def remove
      remove_instance_variable(:@var)
    end
  end

  tri = Test4RemoveInstanceVar.new
  assert_equal 99, tri.var
  assert_equal 99, tri.remove
  assert_equal nil, tri.var
  assert_raise(NameError) { tri.remove }
  assert_raise(NameError) { tri.remove_instance_variable(:var) }
  assert_raise(FrozenError) { tri.freeze.remove }
  assert_raise(FrozenError, NameError) { :a.remove_instance_variable(:@v) }
end

# Kernel#require is defined in mruby-require. '15.3.1.3.42'

assert('Kernel#respond_to?', '15.3.1.3.43') do
  class Test4RespondTo
    def valid_method; end

    def test_method; end
    undef test_method
  end

  assert_raise TypeError do
    Test4RespondTo.new.respond_to?(1)
  end

  assert_raise ArgumentError do
    Test4RespondTo.new.respond_to?
  end

  assert_raise ArgumentError do
    Test4RespondTo.new.respond_to? :a, true, :aa
  end

  assert_true respond_to?(:nil?)
  assert_true Test4RespondTo.new.respond_to?(:valid_method)
  assert_true Test4RespondTo.new.respond_to?('valid_method')
  assert_false Test4RespondTo.new.respond_to?(:test_method)
end

assert('Kernel#respond_to? with an unimplemented method') do
  obj = TestNotImplement.new

  assert_false obj.respond_to?(:gone)
  assert_false obj.respond_to?(:gone, true)
  assert_false TestNotImplement.respond_to?(:gone)
  assert_false TestNotImplement.method_defined?(:gone)

  # The method is defined all the same: calling it says why it is not there,
  # rather than reporting no such method.
  assert_raise(NotImplementedError) { obj.gone }
  assert_raise(NotImplementedError) { TestNotImplement.gone }
end

assert('Kernel#respond_to? skips respond_to_missing? for an unimplemented method') do
  cls = Class.new(TestNotImplement) do
    def respond_to_missing?(name, priv = false)
      true
    end
  end

  # A method that exists leaves respond_to_missing? nothing to answer.
  assert_false cls.new.respond_to?(:gone)
  assert_true cls.new.respond_to?(:no_such_method)

  # Hiding it changes nothing: what the build does not implement is answered
  # before visibility is weighed, at either visibility asked for.
  hidden = Class.new(TestNotImplement) do
    private :gone
    def respond_to_missing?(name, priv = false)
      true
    end
  end
  assert_false hidden.new.respond_to?(:gone)
  assert_false hidden.new.respond_to?(:gone, true)
end

class RespondToVisibility
  def pub; end
  private def priv; end
  protected def prot; end

  def later; end
  def again; end
  private :later, :again
  public :again

  alias_method :priv_alias, :priv

  def self.smeth; end
  class << self
    private def shidden; end
  end
end

class RespondToVisibilityChild < RespondToVisibility; end

module RespondToVisibilityModule
  def mpub; end
  private def mpriv; end
  protected def mprot; end
end

class RespondToVisibilityIncluder
  include RespondToVisibilityModule
end

module RespondToVisibilityPrepended
  private def ppriv; end
end

class RespondToVisibilityPrepender
  prepend RespondToVisibilityPrepended
end

assert('Kernel#respond_to? weighs how the method may be called') do
  obj = RespondToVisibility.new

  assert_true  obj.respond_to?(:pub)
  assert_true  obj.respond_to?(:pub, true)

  # A method a call with a receiver cannot reach is not one the object
  # responds to, unless the second parameter asks for it.
  assert_false obj.respond_to?(:priv)
  assert_true  obj.respond_to?(:priv, true)
  assert_false obj.respond_to?(:prot)
  assert_true  obj.respond_to?(:prot, true)

  # the visibility a name carries is what counts, however it got there,
  # and it is the one it carries now
  assert_false obj.respond_to?(:later)
  assert_true  obj.respond_to?(:later, true)
  assert_true  obj.respond_to?(:again)
  assert_false obj.respond_to?(:priv_alias)
  assert_true  obj.respond_to?(:priv_alias, true)

  # a singleton method is public where the surrounding scope said nothing,
  # and private where its own class body said so
  assert_true  RespondToVisibility.respond_to?(:smeth)
  assert_false RespondToVisibility.respond_to?(:shidden)
  assert_true  RespondToVisibility.respond_to?(:shidden, true)

  # the answer comes from wherever the method is found
  child = RespondToVisibilityChild.new
  assert_false child.respond_to?(:priv)
  assert_true  child.respond_to?(:priv, true)

  includer = RespondToVisibilityIncluder.new
  assert_true  includer.respond_to?(:mpub)
  assert_false includer.respond_to?(:mpriv)
  assert_true  includer.respond_to?(:mpriv, true)
  assert_false includer.respond_to?(:mprot)
  assert_true  includer.respond_to?(:mprot, true)

  prepender = RespondToVisibilityPrepender.new
  assert_false prepender.respond_to?(:ppriv)
  assert_true  prepender.respond_to?(:ppriv, true)

  # the private methods mruby itself defines answer the same way
  assert_false obj.respond_to?(:initialize)
  assert_true  obj.respond_to?(:initialize, true)
  assert_false obj.respond_to?(:method_missing)
  assert_false obj.respond_to?(:respond_to_missing?)
end

class RespondToHidden
  private def hidden; end

  def asked; @asked; end

  def respond_to_missing?(name, include_private = false)
    @asked = [name, include_private]
    name == :hidden
  end
end

class RespondToRefused < RespondToHidden
  def respond_to_missing?(name, include_private = false)
    @asked = [name, include_private]
    false
  end
end

assert('Kernel#respond_to? leaves a method out of reach to respond_to_missing?') do
  obj = RespondToHidden.new
  assert_true obj.respond_to?(:hidden)
  assert_equal [:hidden, false], obj.asked

  # the second parameter is handed over as it was given
  refused = RespondToRefused.new
  assert_false refused.respond_to?(:hidden)
  assert_equal [:hidden, false], refused.asked

  # a method the second parameter reaches is answered without asking
  reached = RespondToRefused.new
  assert_true reached.respond_to?(:hidden, true)
  assert_nil reached.asked
end

class RespondToMissingUndefined
  def pub; end
  private def priv; end
  undef_method :respond_to_missing?
end

class RespondToMissingClaims
  def respond_to_missing?(name, include_private = false)
    true
  end
end

class RespondToMissingStopped < RespondToMissingClaims
  undef_method :respond_to_missing?
end

assert('Kernel#respond_to? answers false where respond_to_missing? is undefined') do
  obj = RespondToMissingUndefined.new
  # a name with no method behind it is false at either visibility asked for,
  # rather than an error about `respond_to_missing?` itself
  assert_false obj.respond_to?(:no_such_method)
  assert_false obj.respond_to?(:no_such_method, true)

  # the methods that are there answer as before
  assert_true obj.respond_to?(:pub)
  assert_false obj.respond_to?(:priv)
  assert_true obj.respond_to?(:priv, true)

  # the undefinition stops the lookup, so a superclass that would have
  # claimed the name is not asked either
  assert_true RespondToMissingClaims.new.respond_to?(:no_such_method)
  assert_false RespondToMissingStopped.new.respond_to?(:no_such_method)
end

assert('an unimplemented method can be overridden, aliased and undefined') do
  overridden = Class.new(TestNotImplement) do
    def gone
      :here
    end
  end
  assert_true overridden.new.respond_to?(:gone)
  assert_equal :here, overridden.new.gone

  aliased = Class.new(TestNotImplement) do
    alias_method :also_gone, :gone
  end
  assert_false aliased.new.respond_to?(:also_gone)
  assert_raise(NotImplementedError) { aliased.new.also_gone }

  undefined = Class.new(TestNotImplement) do
    undef_method :gone
  end
  assert_false undefined.new.respond_to?(:gone)
  assert_raise(NoMethodError) { undefined.new.gone }
end

assert('an unimplemented method reports itself by name') do
  assert_raise_with_message(NotImplementedError,
                            'gone() function is unimplemented on this machine') do
    TestNotImplement.new.gone
  end
end

assert('mrb_notimplement() raises where there is no method name to report') do
  # The name comes from the frame the call is made on, and a call that does not
  # come from a method has none. Ruby cannot build such a frame; TestNotImplement
  # makes the call from its own init function and records what came back.
  assert_true TestNotImplement::NAMELESS_RAISED
  assert_kind_of NotImplementedError, TestNotImplement::NAMELESS_RESULT
  assert_equal 'function is unimplemented on this machine',
               TestNotImplement::NAMELESS_RESULT.message
end

assert('Kernel#to_s', '15.3.1.3.46') do
  assert_equal to_s.class, String
end

assert('Kernel#!=') do
  str1 = "hello"
  str2 = str1
  str3 = "world"

  assert_false (str1[1] != 'e')
  assert_true (str1 != str3)
  assert_false (str2 != str1)
end

# operator "!~" is defined in ISO Ruby 11.4.4.
assert('Kernel#!~') do
  x = "x"
  def x.=~(other)
    other == "x"
  end
  assert_false x !~ "x"
  assert_true  x !~ "z"

  y = "y"
  def y.=~(other)
    other == "y"
  end
  def y.!~(other)
    other == "not y"
  end
  assert_false y !~ "y"
  assert_false y !~ "z"
  assert_true  y !~ "not y"

  # the answer is a truth value read off what `=~` returned, not that value
  z = "z"
  def z.=~(other)
    other == "z" ? 0 : nil
  end
  assert_false z !~ "z"
  assert_true  z !~ "not z"

  # an exception raised by `=~` is the answer to `!~`
  e = "e"
  def e.=~(other)
    raise ArgumentError, "no match for you"
  end
  assert_raise(ArgumentError) { e !~ "e" }

  # `=~` may ask another object the same question while answering
  class Test4NotMatchPeer
    def =~(other)
      other == "b"
    end
  end
  class Test4NotMatch
    def =~(other)
      Test4NotMatchPeer.new !~ other
    end
  end
  assert_true  Test4NotMatch.new !~ "b"
  assert_false Test4NotMatch.new !~ "c"
end

assert('Kernel#respond_to_missing?') do
  class Test4RespondToMissing
    def respond_to_missing?(method_name, include_private = false)
      method_name == :a_method
    end
  end

  assert_true Test4RespondToMissing.new.respond_to?(:a_method)
  assert_false Test4RespondToMissing.new.respond_to?(:no_method)
end

assert('stack extend') do
  def recurse(count, stop)
    return count if count > stop
    recurse(count+1, stop)
  end

  assert_equal 6, recurse(0, 5)
end

assert('Kernel module functions') do
  # the qualified form reaches the module function
  assert_raise(ArgumentError) { Kernel.raise ArgumentError }
  assert_false Kernel.block_given?
  assert_false Kernel.iterator?

  # while the instance-side copies stay private
  obj = Object.new
  assert_raise(NoMethodError) { obj.raise ArgumentError }
  assert_raise(NoMethodError) { obj.block_given? }
  assert_raise(NoMethodError) { obj.loop }

  # and unrelated modules do not gain them
  assert_raise(NoMethodError) { Comparable.raise ArgumentError }
end