On Tuesday 19 October 2004 09:59 am, Eivind Eklund wrote:
| Sure.  From a practical use of Florian's code (his sample from
| Regexp::English):
|
|      # Creates a Regexp which matches a literal string. In this
|      # string any special regular expression meta-characters will
|      # be escaped automatically.
|      #
|      #   # This creates a Regexp which will match 3 "foo"s.
|      #   re = Regexp::English.literal("foo" * 3)
|      #   re.match("foofoofoo")[0] # => "foofoofoo"
|      def literal(text); Node::Literal.new(text); end
|
| Translated to Nathaniel's syntax:
|
|      # Creates a Regexp which matches a literal string. In this
|      # string any special regular expression meta-characters will
|      # be escaped automatically.
|      example do
|         # This creates a Regexp which will match 3 "foo"s.
|         re = Regexp::English.literal("foo" * 3)
|         assert_equal("foofoofoo", re.match("foofoofoo")[0])
|      end
|      def literal(text); Node::Literal.new(text); end

This points out the distinction nicely --and the slight dislike I have with 
both approaches --Florian's is comment and Nathaniel's is not an example but 
a test. I would like things to be what they are.

      # Creates a Regexp which matches a literal string. In this
      # string any special regular expression meta-characters will
      # be escaped automatically.
      example do
         # This creates a Regexp which will match 3 "foo"s.
         re = Regexp::English.literal("foo" * 3)
         re.match("foofoofoo")[0] # => "foofoofoo"
      end
      def literal(text); Node::Literal.new(text); end

But I also ask, how can one move the example to some place else in the code? 
Wouldn't it be good to label them? Then something like this is possible:

      # Creates a Regexp which matches a literal string. In this
      # string any special regular expression meta-characters will
      # be escaped automatically.
      def literal(text); Node::Literal.new(text); end

      example :literal do
         # This creates a Regexp which will match 3 "foo"s.
         re = Regexp::English.literal("foo" * 3)
         re.match("foofoofoo")[0] # => "foofoofoo"
      end

      test :literal do
         re = Regexp::English.literal("foo" * 3)
         assert_equal("foofoofoo", examples[:literal].call)
      end

T.