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 ambiguous:
Binary Minus vs Minus taken as Unary Method Argument
>> [1,3,4,5].size - 1
# => 3
>> [1,3,4,5].size -1
# wrong number of arguments(1 for 0)
No Simple Rule, if a Symbol can be Displayed Without the Explicit :""
Syntax
>> {:< => 0}
# => {:<=>0}
>> {:<=>0}
# syntax error, unexpected tINTEGER,
# expecting tASSOC
Ambiguous if to Parse ~
Unary or as Part of Binary Match Operator
>> $_="Eurucamp X"
>> ~/X/ # => 9
>> a=~/X/ # undefined local variable or method `a'
>> a= ~/X/ # => 9
Global Variables can "Break" String and Regex Syntax
>> a = "Eurucamp #\n"
>> a.gsub /#$/, ''
# => ?
>> a = "Eurucamp #\n"
>> a.gsub /#$/, ''
# unterminated regexp meets end of file
>> a = "Eurucamp #\n"
>> a.gsub /#$//, ''
# => "Eurucamp #"
Unary or Binary Plus, Part 2
>> p +21
# => 21
>> p = 21
>> p +21
# => 42
String Creation vs Format Method
>> puts%[1]
# undefined method `%' for nil:NilClass
>> puts %[1]
# => 1
Hash vs Block
Ruby uses curly braces for both, blocks as well as hashes. Sometimes, this leads to confusing cases:
>> def identity(a) a end
>> identity(1)
#=> 1
>> identity 1
#=> 1
>> identity({})
#=> {}
>> identity {}
#=> wrong number of arguments (0 for 1) (ArgumentError)
Regex vs Division
>> puts /4/i
# => (?i-mx:4)
>> puts, i = 42, 2
>> puts /4/i
# => 5
Further Reading
More Idiosyncratic Ruby
- Please Comment on GitHub
- Next Article: Regex with Class
- Previous Article: Operating Nothing