Commits

David Chambers  committed 78dcb84

prefix CoffeeScript code blocks with `:::coffeescript`

  • Participants
  • Parent commits ddd8284

Comments (0)

Files changed (1)

 
 Placeholders may contain numbers which refer to positional arguments:
 
+    :::coffeescript
     "{0}, you have {1} unread message{2}".format("Holly", 2, "s")
     # "Holly, you have 2 unread messages"
 
 Unmatched placeholders produce no output:
 
+    :::coffeescript
     "{0}, you have {1} unread message{2}".format("Steve", 1)
     # "Steve, you have 1 unread message"
 
 A format string may reference a positional argument multiple times:
 
+    :::coffeescript
     "{0} x {0} x {0} = {1}".format(3, 3*3*3)
     # "3 x 3 x 3 = 27"
 
 Positional arguments may be referenced implicitly:
 
+    :::coffeescript
     "{}, you have {} unread message{}".format("Steve", 1)
     # "Steve, you have 1 unread message"
 
 A format string must not contain both implicit and explicit references:
 
+    :::coffeescript
     "My name is {} {}. Do you like the name {0}?".format("Lemony", "Snicket")
     # ERROR: cannot switch from implicit to explicit numbering
 
 `{{` and `}}` in format strings produce `{` and `}`:
 
+    :::coffeescript
     "{{}} creates an empty {} in {}".format("dictionary", "Python")
     # "{} creates an empty dictionary in Python"
 
 Dot notation may be used to reference object properties:
 
+    :::coffeescript
     bobby = first_name: "Bobby", last_name: "Fischer"
     garry = first_name: "Garry", last_name: "Kasparov"
     
 
 When referencing the first positional argument, `0.` may be omitted:
 
+    :::coffeescript
     repo = {owner: "pypy", slug: "pypy", followers: [...]}
     
     "{owner}/{slug} has {followers.length} followers".format(repo)
 
 “Transformers” can be attached to `String.prototype.format.transformers`:
 
+    :::coffeescript
     String::format.transformers.upper = -> @toUpperCase()
     
     "Batman's preferred onomatopoeia: {0!upper}".format("pow!")
 `toString` method, so transformers may be used in conjunction with non-string
 objects:
 
+    :::coffeescript
     peter_parker =
       first_name: "Peter"
       last_name: "Parker"
 
 A transformer could sanitizing untrusted input:
 
+    :::coffeescript
     String::format.transformers.escape = ->
       @replace /[&<>"'`]/g, (chr) -> "&#" + chr.charCodeAt(0) + ";"
     
 
 Or pluralize nouns, perhaps:
 
+    :::coffeescript
     String::format.transformers.s = -> "s" unless +this is 1
     
     "{0}, you have {1} unread message{1!s}".format("Holly", 2)
 a variable to avoid repetition. The idiomatic alternative is to invoke
 `String::format` with no arguments, which produces a reusable function:
 
+    :::coffeescript
     greet = "{0}, you have {1} unread message{1!s}".format()
     
     greet("Holly", 2)