lib/facet/kernel/demo.rb in facets-0.9.0 vs lib/facet/kernel/demo.rb in facets-1.0.0
- old
+ new
@@ -1,45 +1,90 @@
-require 'nano/kernel/demo.rb'
\ No newline at end of file
+module Kernel
+ module_function
+ # For debugging and showing examples. Currently this
+ # takes an argument of a string in a block.
+ #
+ # demo {%{ a = [1,2,3] }}
+ # demo {%{ a.slice(1,2) }}
+ # demo {%{ a.map { |x| x**3 } }}
+ #
+ # Produces:
+ #
+ # a = [1,2,3] #=> [1, 2, 3]
+ # a.slice(1,2) #=> [2, 3]
+ # a.map { |x| x**3 } #=> [1, 8, 27]
+ #
+ #--
+ # Is there a way to do this without the eval string in block?
+ # Preferably just a block and no string.
+ #++
+ def demo(out=$stdout,&block)
+ out << sprintf("%-25s#=> %s\n", expr = block.call, eval(expr, block.binding).inspect)
+ end
+end
+
+
+# _____ _
+# |_ _|__ ___| |_
+# | |/ _ \/ __| __|
+# | | __/\__ \ |_
+# |_|\___||___/\__|
+#
+=begin test
+
+ require 'test/unit'
+
+ class TCKernel < Test::Unit::TestCase
+
+ def test_demo
+ demo(i=''){%{ a = [1,2,3] }}
+ o = " a = [1,2,3] #=> [1, 2, 3]\n"
+ assert_equal( o, i )
+ end
+
+ end
+
+=end