Skip to content

Commit

Permalink
Use .. as secondary prompt for ruby
Browse files Browse the repository at this point in the history
This is a non backward compatibility change.

Before, one or more *consecutive* primary prompts >> were considered a
single example.

This is too fragile and dangerous.

Consider the following (previous this commit)

>> puts "a"     # byexample: +pass
>> puts "b"

Which output is none: the +pass is affecting the 2-lines example.

Now, if you add a single newline (probably for aesthetics purposes)

>> puts "a"     # byexample: +pass

>> puts "b"

The example is split in 2 and the second is has no the +pass option
enabled.

For this reason now each >> is a single example; for multiline examples
we need to use ..

We go from this

>> def f
>>   1
>> end

to this

>> def f
..   1
.. end
  • Loading branch information
eldipa committed Jul 6, 2018
1 parent 0d316e7 commit 9784f38
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 41 deletions.
10 changes: 5 additions & 5 deletions byexample/modules/ruby.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"""
Example:
>> def hello
>> 'hello bla world'
>> end;
.. 'hello bla world'
.. end;
>> hello
=> "hello<...>world"
Expand Down Expand Up @@ -53,13 +53,13 @@ class RubyPromptFinder(ExampleFinder):
@constant
def example_regex(self):
return re.compile(r'''
# Snippet consists of one or more PS1 lines >>
# Snippet consists of one PS1 line >> and zero or more PS2 lines
(?P<snippet>
(?:^(?P<indent> [ ]*) >>[ ] .*) # PS1 line
(?:\n [ ]* >> .*)*) # and more PS1 lines
(?:\n [ ]* \.\. .*)*) # zero or more PS2 lines
\n?
# Want consists of any non-blank lines that do not start with PS1
# The '=>' indicator is included (implicitly)
# The '=>' indicator is included (implicitly) and may not exist
(?P<expected> (?:(?![ ]*$) # Not a blank line
(?![ ]*>>) # Not a line starting with PS1
.+$\n? # But any other line
Expand Down
41 changes: 7 additions & 34 deletions docs/languages/ruby.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,8 @@ Check its [download page](https://www.ruby-lang.org/en/downloads/)

## Find interactive examples

For ``Ruby``, ``byexample`` uses the ``>>`` string as the primary and
secondary prompts.

Because of this, all the consecutive lines that start with ``>>`` will belong
to the same example and they will be executed together.
For ``Ruby``, ``byexample`` uses the ``>>`` string as the primary prompt
and ``..`` as the secondary prompt.


```ruby
Expand All @@ -21,11 +18,11 @@ to the same example and they will be executed together.
=> 3

>> def g(a, b, c)
>> c += a
>> c += b
>>
>> return c
>> end
.. c += a
.. c += b
..
.. return c
.. end

>> g(1, 2, 3)
=> 6
Expand Down Expand Up @@ -99,30 +96,6 @@ semicolons. It is easy to get confused with this weird effect.

```

This is important because you may want to use ``;`` to suppress the expression's
value in a multi expression example:


```ruby
>> a = 1;
>> b = 2;
>> a + b
=> 3

```

In contrast with:

```ruby
>> a = 1
>> b = 2
>> a + b
=> 1
=> 2
=> 3

```

# Pretty print

``byexample`` changes the default IRB's ``inspector`` and uses ``pp``
Expand Down
4 changes: 2 additions & 2 deletions test/test.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ File docs/languages/cpp.md, 2/2 test ran in <...> seconds
File docs/languages/gdb.md, 8/8 test ran in <...> seconds
[PASS] Pass: 8 Fail: 0 Skip: 0
~
File docs/languages/ruby.md, 12/12 test ran in <...> seconds
[PASS] Pass: 12 Fail: 0 Skip: 0
File docs/languages/ruby.md, 14/14 test ran in <...> seconds
[PASS] Pass: 14 Fail: 0 Skip: 0
~
File docs/languages/python.md, 41/41 test ran in <...> seconds
[PASS] Pass: 40 Fail: 0 Skip: 1
Expand Down

0 comments on commit 9784f38

Please sign in to comment.