Permalink
Browse files

Add 29: "Limitations of Language"

  • Loading branch information...
janlelis committed May 29, 2015
1 parent c541f1e commit cdc80a62c207f966f5391f36ce9767ed41c7beb3
Showing with 87 additions and 0 deletions.
  1. +87 −0 source/posts/29-limitations-of-language.html.md
@@ -0,0 +1,87 @@
---
title: Limitations of Language
date: 2015-05-29
tags: syntax, grammar
---

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:

ARTICLE

## 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
## Different Rules for Modules and Classes

>> module Eurucamp end
# => nil
>> class Eurucamp end
# syntax error, unexpected $end
## 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
>> p 21
# => 21
>> p = 21
>> p +21
# => 42
## String Creation vs Format Method
>> puts%[1]
# undefined method `%' for nil:NilClass
>> puts %[1]
# => 1
## Regex vs Division
>> puts /4/i
# => (?i-mx:4)
>> puts, i = 42, 2
>> puts /4/i
# => 5
## Further Reading
- [Ruby's grammar definition](https://github.com/ruby/ruby/blob/trunk/parse.y)

0 comments on commit cdc80a6

Please sign in to comment.