Created
September 15, 2012 14:53
-
-
Save davybrion/3728350 to your computer and use it in GitHub Desktop.
code snippets for "Using Ruby Classes In C# With IronRuby" post, part II
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
if defined? IronRuby | |
def add_dotnet_friendly_method_aliases_for(klass) | |
klass.public_instance_methods.each do |method| | |
klass.instance_eval do | |
dotnet_friendly_name = IronRuby::Clr::Name.unmangle(method) | |
alias_method dotnet_friendly_name, method unless dotnet_friendly_name.nil? # in this case, it's already a dotnet friendly name | |
end | |
end | |
end | |
add_dotnet_friendly_method_aliases_for Product | |
add_dotnet_friendly_method_aliases_for Customer | |
add_dotnet_friendly_method_aliases_for OrderItem | |
add_dotnet_friendly_method_aliases_for Order | |
end |
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
var engine = Ruby.CreateEngine(); | |
engine.ExecuteFile("bootstrap.rb"); | |
dynamic ruby = engine.Runtime.Globals; | |
dynamic customer = ruby.Customer.@new("Davy Brion", "[email protected]"); | |
dynamic product1 = ruby.Product.@new("product1", 50); | |
dynamic product2 = ruby.Product.@new("product2", 60); | |
dynamic order = ruby.Order.@new(customer, null, DateTime.Now); | |
order.AddItem(ruby.OrderItem.@new(product1, 5)); | |
order.AddItem(ruby.OrderItem.@new(product2, 5)); | |
var total = 0; | |
foreach (dynamic item in order.Items) | |
{ | |
total += item.Count * item.Product.Price; | |
} | |
Console.WriteLine("order total: " + total); | |
Console.WriteLine(); | |
Console.WriteLine(order.Inspect()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment