oftn/oftn-bot — pull request #34 by imbcmdth (2014-02-15 18:54:50+00:00)
Title: Self-executing factoids with code (codetoids)

This is a feature that @gkatsev and I worked on a while back. It allows one to make a factoid with code that will be executed and the results posted just like a regular line of code. The idea was to turn commonly used code examples into simple factoids.

Another constraint was to have it look exactly like a regular execute command so that the user can copy the line exactly as-is, alter it, and have it execute as expected. Explanation/instructions would follow the code in a comment.

An example is a factoid explaining Array#map:

````
!learn Array#map = >> [1, 2, 3, 4].map(function(n) { return n * 2; }); // Array#map applies a function to each element in an array and returns a new array with the results. In this case, we double each number.
````

When invoked as a normal factoid:

````
!Array#map @foo
````

The bot will respond with:

````
>> [1, 2, 3, 4].map(function(n) { return n * 2; }); // Array#map applies a function to each element in an array and returns a new array with the results. In this case, we double each number. @foo
foo: (object) [2, 4, 6, 8]
````

Since it uses the same machinery as the regular execute command, you can use a specific language/engine by properly prefixing the factoid with the right execute prefix.

````
!learn V8 = >>> version;
ImBcmDth: Learned `V8`.
!v8
>>> version; @imbcmdth
imbcmdth: (string) 'V8 3.11.10.10'
````