Datasheet
Book VIII
Chapter 1
Programming
Dynamically!
777
Running with the Dynamic Language Runtime
Figure 1-1:
The
Dynamic
Language
Runtime.
The runtime helps the compiler to construct code in the compiled assembly
that will make a lot of choices dynamically. The code block at the end of the
preceding section is an example of the simplest kind.
The DLR assisted in the creation of IRONRuby, which makes it possible to
code in Ruby — the current hot dynamic language — right in Visual Studio.
Of course, because the DLR enables C# to take on dynamic language fea-
tures, much that you can do in Ruby you can now do in C#.
Dynamic Ruby
Ruby takes advantage of its dynamic roots in its implementation of the
Trabb Pardo-Knuth algorithm. Don’t be put off by the name — this is just a
straightforward problem that can be solved by computer code.
The program needs to read 11 numbers from an input device — in our case,
the console’s ReadLine method. It stores them in an array. Then, it pro-
cesses the array backward — starting from the last entered value — with
some function. If the value doesn’t exceed some arbitrary threshold, it prints
the result.
The program looks like this in Ruby:
class TPK
def f( x )
return Math.sqrt(x.abs) + 5*x **3
end
53_563489-bk08ch01.indd 77753_563489-bk08ch01.indd 777 3/19/10 8:17 PM3/19/10 8:17 PM