Ruby Quiz #1 - Bonus - Three More Difficulty Levels (2, 3, 4) - Commas Inside Quotes and Double Up Quotes in Quotes, ...

Gerald Bauer <[email protected]>
Newsgroups gmane.comp.lang.ruby.general
Message-ID <CAAxEZd8O+VzJE3TyOycqTnxpUu5wpW2hkeJM1qeOZekbzYH49Q@mail.gmail.com>
Hello,

    Thanks for the first three entries - all passing the level 1 test. Yay!

    If that was too easy :-)  I added three more difficulty levels
  to the challenge.[1]
.

### Level 2 - Commas Inside Quotes and Double Up Quotes in Quotes

Let's turn Shakespeare's "literal" Hamlet quote:

```
Hamlet says, "Seems," madam! Nay it is; I know not "seems."
```

into

```
1, "Hamlet says, ""Seems,"" madam! Nay it is; I know not ""seems."""
```

And the test reads:

``` ruby
def test_parse_level2
  records = [["1", "Hamlet says, \"Seems,\" madam! Nay it is; I know
not \"seems.\""]]

  assert_equal records, parse( <<TXT )
1, "Hamlet says, ""Seems,"" madam! Nay it is; I know not ""seems."""
TXT
end
```


### Level 3 - Unix/Ruby-Style Backslash Escapes

Lets add the "unix-style" escaping with backslashes (e.g. `\"` for `""`)
used by Ruby :-), PostgreSQL, MySQL and others
(when exporting database tables in CSV, for example):

```
1, "Hamlet says, \"Seems,\" madam! Nay it is; I know not \"seems.\""
```

And the test reads:

``` ruby
def test_parse_level3
  records = [["1", "Hamlet says, \"Seems,\" madam! Nay it is; I know
not \"seems.\""]]

  assert_equal records, parse( <<TXT )
1, "Hamlet says, \\"Seems,\\" madam! Nay it is; I know not \\"seems.\\""
TXT
end
```



### Level 4 - Single or Double? Mixed Quotes

Let's again get inspired by Ruby :-) -
see 210 Ways to Rome [2] and lets add single or double quotes.

```
1, "Hamlet says, 'Seems,' madam! Nay it is; I know not 'seems.'"
2, 'Hamlet says, "Seems," madam! Nay it is; I know not "seems."'
```

And the test reads:

``` ruby
def test_parse_level4
  records = [[1, "Hamlet says, 'Seems,' madam! Nay it is; I know not 'seems.'"],
             [2, 'Hamlet says, "Seems," madam! Nay it is; I know not "seems."']]

  assert_equal records, parse( <<TXT )
1, "Hamlet says, 'Seems,' madam! Nay it is; I know not 'seems.'"
2, 'Hamlet says, "Seems," madam! Nay it is; I know not "seems."'
TXT
end
```


Happy hacking and data wrangling with Ruby.

[1] https://github.com/planetruby/quiz
[2] https://idiosyncratic-ruby.com/15-207-ways-to-rome.html

Unsubscribe: <mailto:[email protected]?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.