Skip to content

Commit cdc80a6

Browse files
committed
Add 29: "Limitations of Language"
1 parent c541f1e commit cdc80a6

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
---
2+
title: Limitations of Language
3+
date: 2015-05-29
4+
tags: syntax, grammar
5+
---
6+
7+
If you take a closer look, you'll notice that Ruby's grammar has quite a few edge-case where its syntax is inconsistent or ambigious:
8+
9+
ARTICLE
10+
11+
## Binary Minus vs Minus taken as Unary Method Argument
12+
13+
>> [1,3,4,5].size - 1
14+
# => 3
15+
16+
>> [1,3,4,5].size -1
17+
# wrong number of arguments(1 for 0)
18+
19+
## No Simple Rule, if a Symbol can be Displayed Without the Explicit `:""` Syntax
20+
21+
>> {:< => 0}
22+
# => {:<=>0}
23+
24+
>> {:<=>0}
25+
# syntax error, unexpected tINTEGER,
26+
# expecting tASSOC
27+
28+
## Different Rules for Modules and Classes
29+
30+
>> module Eurucamp end
31+
# => nil
32+
33+
>> class Eurucamp end
34+
# syntax error, unexpected $end
35+
36+
## Ambiguous if to Parse `~` Unary or as Part of Binary Match Operator
37+
38+
>> $_="Eurucamp X"
39+
>> ~/X/ # => 9
40+
41+
>> a=~/X/ # undefined local variable or method `a'
42+
>> a= ~/X/ # => 9
43+
44+
## Global Variables can "Break" String and Regex Syntax
45+
46+
>> a = "Eurucamp #\n"
47+
>> a.gsub /#$/, ''
48+
# => ?
49+
50+
>> a = "Eurucamp #\n"
51+
>> a.gsub /#$/, ''
52+
# unterminated regexp meets end of file
53+
54+
>> a = "Eurucamp #\n"
55+
>> a.gsub /#$//, ''
56+
# => "Eurucamp #"
57+
58+
## Unary or Binary Plus, Part 2
59+
60+
>> p = 21
61+
>> p 21
62+
# => 21
63+
64+
>> p = 21
65+
>> p +21
66+
# => 42
67+
68+
## String Creation vs Format Method
69+
70+
>> puts%[1]
71+
# undefined method `%' for nil:NilClass
72+
73+
>> puts %[1]
74+
# => 1
75+
76+
## Regex vs Division
77+
78+
>> puts /4/i
79+
# => (?i-mx:4)
80+
81+
>> puts, i = 42, 2
82+
>> puts /4/i
83+
# => 5
84+
85+
## Further Reading
86+
87+
- [Ruby's grammar definition](https://github.com/ruby/ruby/blob/trunk/parse.y)

0 commit comments

Comments
 (0)