Monday, June 22, 2009

Markdown

Markdown is a simple syntax for writing text documents. It is better than HTML since it does not contain as much clutter. HTML may be used in a Markdown document but block level elements must be surrounded with blank lines.

Headers

Headers come in two types.

  • Underlined headers, =====, and -----
  • Prefix headers, which uses # as prefix, # = h1, and ### = h3

Emphasis

  • Emphasis can be shown with * or with _ .
  • If you double them up they will become strong instead ** and __

List

  • Unordered lists are indicated with *, + or -
  • Ordered lists are indicated with number followed by period, 1.

Lists may be span several lines and may also use hanging indents. To wrap the list items in paragraphs simply leave a blank line between item lines.

Blockquotes

Blockquotes are indicated by prefixing lines with >. They may be nested > > . If you only prefix the first line in a paragraph, everything up to the next blank line will be part of the blockquote.

Code

Markdown produces literal markup if the lines are indented with one tab or four spaces. This kind of code will be wrapped with <pre><code>

Lines

Lines may be indicated by putting three or more asterisks (***) or hyphens (---) on a line by itself.

Links

Markdown support both inline and reference links.

Inline links

  • Absolute [text for link](http://url.to.site)
  • Relative [text for link](relative_url)
  • With title [text for link](relative_url "my title")

Reference links

  • [text for link][anders]
  • Same text and id [anders][]

The referenced link ids must be defined somewhere else in the document like this:

[anders]: http:/anders.janmyr.com "Optional title"

The link definitions are inserted into the actual links and removed after the page is processed.

Automatic links

URLs surrounded with angle brackets <> are automatically inserted as links this may be used both for links and email:

  • <anders.janmyr@email.com>
  • <http://anders.janmyr.com>

Images

Images are inserted with link syntax, preceded with an exclamation point !.

  • ![Alt text](/path/to/img.jpg)
  • ![Alt text](/path/to/img.jpg "Optional title")
  • ![Alt text][id]

Escapes

Most of the Markdown special characters such as * and - may be escaped by preceding them with a backslash \.

Monday, June 08, 2009

Ruby Method Lookup

There are three different kinds of methods containers in Ruby, Classes, Modules and Singleton Classes. When a method is invoked all of them are searched for the first matching method to execute.

A class may contain methods

class Mammal 
  def breathe
  end
end

A class may inherit its methods from one other class.

class Tapir < Mammal
end

A class may also mix in methods from several modules.

class Tapir 
  include Swimmable, Runnable
end

Methods may also be defined on the object itself, this is actually the anonymous Singleton Class that is available to every object.

kjell = Tapir.new
# kjell can not only swim, he can crawl
def kjell.crawl 
end

So what happens if all these different method containers define the same method? How will the interpreter know what to execute?

When a method is called:
  • The Singleton Class is searched. —the anonymous class of kjell
  • The modules included in the Singleton Class is searched. —in this case none.
  • The class is searched. —this is the Tapir class
  • The modules included in the class is searched in reverse order of inclusion, the modules override each other as they are included. —Runnable, then Searchable
  • Then the search continuous in the superclass of the class and the modules included in it.
  • If the search reaches the top of the hierarchy and no method is found, method_missing is invoked on the initial object and the search for method_missing follows the same path.

That’s almost it. There is one caveat. If the Singleton Class is a meta-class, that is the singleton class of a class, then the class belongs to a meta-class hierarchy that parallels the hierarchy of the normal classes. This allows for inheritance of class methods and gives us the ability to override class methods such as new. The lookup path is changed to search the meta-class’ superclass after step two, before the class of the class (i.e. instance methods of class Class).