Created
October 22, 2009 08:00
-
-
Save jschementi/215817 to your computer and use it in GitHub Desktop.
IronRuby and C# 4 "dynamic"
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using IronRuby; | |
public class dynamic_demo { | |
static void Main() { | |
var rb = Ruby.CreateEngine(); | |
rb.ExecuteFile("mock.rb"); | |
dynamic globals = rb.Runtime.Globals; | |
// Call the Ruby "Mock" class constructor | |
dynamic m = globals.Mock.@new(); | |
//The Ruby Mock instance dynamically responds to any member that is requested of it | |
Console.WriteLine( | |
m.the_csharp_compiler_cannot_possbily_know_this_method_exists_at_compile_time() | |
); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Mock | |
# instances of this class will dynamically respond to any method | |
def method_missing(method_name) | |
["hello world", Math::PI][rand(2)] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment