Gerald Bauer
2018-10-31 07:42:07 UTC
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:ruby-talk-***@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
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:ruby-talk-***@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>