clarkware / ruby-learning-tests

Unit tests I wrote way back when I started learning Ruby.

This URL has Read+Write access

ruby-learning-tests / metaprogram_test.rb
100755 28 lines (20 sloc) 0.503 kb
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
#!/usr/bin/env ruby
 
require 'test/unit'
 
class MetaprogramTest < Test::Unit::TestCase
 
  def test_method_send
    assert_equal(4, "mike".send(:length))
  end
 
  def test_method_call
    m = "Mike".method(:length)
    assert_equal(4, m.call)
  end
 
  def test_eval
    code = %q{"Mike".length}
    assert_equal(4, eval(code))
  end
 
  def test_new_class
    class_name = "Mike"
    mike_class = instance_eval %{Object::#{class_name} = Class.new(Object)}
    assert_kind_of Object, mike_class
  end
 
end