require 'singleton' module Like end module Like::JavaScript class Undefined include Singleton def to_s 'undefined' end # called by puts def to_ary ['undefined'] end end # Ruby method is not a closure # undefined = Undefined.instance # would be of not much use class ::Object def undefined Undefined.instance end end class Object def method_missing(m) undefined end end class Array < ::Array def [](m) fetch(m, undefined) end end end # gem install minitest require 'minitest/autorun' module Like::JavaScript class UndefinedTest < Minitest::Test def test_variable assert_raises NameError do foo end end def test_to_s assert_equal 'undefined', undefined.to_s end def test_object_missing o = Object.new assert_equal undefined, o.foo end def test_array_missing a = Array.new assert_equal undefined, a[0] end def test_array_exists a = Array.new([:foo]) assert_equal :foo, a[0] end end end